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...

Michael Ohlrogge
  • 10,559
  • 5
  • 48
  • 76
Guruswamy B M
  • 317
  • 1
  • 5
  • 13

3 Answers3

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'}
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 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 – Guruswamy B M Oct 04 '13 at 08:18
  • 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? – Alfe Oct 04 '13 at 21:54
  • 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. – Guruswamy B M Oct 06 '13 at 11:13
  • 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. – Guruswamy B M Oct 06 '13 at 11:25
  • 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. – Alfe Oct 10 '13 at 23:15
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'}}}
xiyurui
  • 225
  • 3
  • 4
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
fragilewindows
  • 1,394
  • 1
  • 15
  • 26
Guruswamy B M
  • 317
  • 1
  • 5
  • 13