14

I have string of data basically which has a objects with in the objects..

{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", group:{"id": "XXXX"}}}. You can check this format using "http://chris.photobooks.com/json/default.html" site.

No my requirement is to convert this to JSON objects as a dictionary. I have tried below way

import json
JSON_Datalist = '{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", group:{"id": "XXXX"}}}'
the_dict = json.loads(JSON_DataList)

but the_dict gives only left side values, no right values...

In the same way if string has a format..

"[{"sample": false, "radop": null, "view": true, "Example1": null}, {"noMarket": false, "Example2": null}]"

and following the same code.

JSON_Datalist = '[{"sample": false, "radop": null, "view": true, "Example1": null}, {"noMarket": false, "Example2": null}]'

the_dict = json.loads(JSON_DataList)

it gives the dictionary of length of 2, and this is what expected...

Can any please help me out in first case how can I get a dictionary...

2
  • 1
    Your JSON is invalid. group needs to be quoted. Commented Oct 1, 2013 at 7:57
  • 2
    protip: If you want valid JSON, do something like this: import json; json.dumps({'key': 'val', 'other_key' : {'something': [1, 2, 3]}}) - let the json library do it for you. Commented Oct 4, 2013 at 22:39

3 Answers 3

8

I found two errors in your first example:

  1. You have a group in your stringified (Json) version of your dict. This should be a "group" (with quotes).
  2. You misspelled your variable; JSON_DatalistJSON_DataList (lowercase vs. capital L).

After fixing both, I had no problems anymore:

>>> JSON_Datalist = '{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", "group":{"id": "XXXX"}}}'
>>> the_dict = json.loads(JSON_Datalist)
>>> the_dict
{u'user': {u'username': u'XYZ', u'group': {u'id': u'XXXX'}, u'id': u'XXXX'}, u'id': u'XXXX', u'name': u'xyz'}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Alfe, Now I want get the value of "id": "XXXX" which is present in user: group how to get this?, In Python we can get the value like this the_dict['user']['group']['id'], this will print ID value. How to do this in program
What do you mean when you mention a difference between "in Python" and "in program"? Just use the_dict['user']['group']['id'] as you proposed. What's the issue with using this?
Thanks..I know the_dict['user']['group']['id'] will give the value, but it is static interpreter command. In run time I don't know the exact command (which key value). EX: it may be dict['user'] or dict['user']['group'] or dict['user']['group']['id']['etc..']['etc..']. So can you suggest me on this case how can I make this interpreter command on run time.
To be elaborate: Say I have function fun1(), which has JSON_Datalist dictionary.. from python I will call this function by key names to get the value. Say I will call the function as fun1(user:group) to get the values. Here the argument can any deep level.
I understand (I think). Please explain how the wanted value is specified. Is it a (the only) leaf in the tree which fits a certain condition? What is this condition? To perform a search in a tree you can use breadth-search or depth-search or a more custom-tailored variant. But I need to know more about the situation you are in to give qualified advice.
5

after fix the problem group should be "group", below code can meet your requirement

json_data={"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", "group":{"id": "XXXX"}}}
data = json.dumps(json_data)
json_to_python = json.loads(data)
print (json_to_python)


{'id': 'XXXX', 'name': 'xyz', 'user': {'id': 'XXXX', 'username': 'XYZ', 'group': {'id': 'XXXX'}}}

Comments

0

I have figured out how to generate the_dict['user']['group']['id'] dynamically through Python's eval expression.

Keys is the input from the user, separated by :. Example: user:group:id.

CODE:

RefCount = 0
RespDict = json.loads(JSON_Datalist.content) #convert strings to Java object using JSON
SplitKeys = Keys.split(":") 
KeyCount = len(SplitKeys)
CommandText = "RespDict" #To setup command line based on keys information
while (RefCount <KeyCount):
    CommandText = CommandText+"['"+SplitKeys[RefCount]+"']"
    RefCount = RefCount + 1
    print CommandText        
print eval(CommandText)  #Final key value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.