Parsing json fields in python

Is there a good tutorial on parsing json attributes in python? I would like to be able to parse the true value for the "ok" field. Just like an index named "client_ind_1". I do not understand the scope of the python document on this topic. If someone can explain or point me to the best resource, it will be awesome.

My json line looks like this:

{ "ok": true, "_shards": { "total": 2, "successful": 1, "failed": 0 }, "indices": { "client_ind_2": { "index": { "primary_size": "2.5mb", "primary_size_in_bytes": 2710326, "size": "2.5mb", "size_in_bytes": 2710326 } } } } 

Thanks in advance.

+7
python django simplejson
source share
2 answers
 import json a = """{ "ok": true, "_shards": { "total": 2, "successful": 1, "failed": 0 }, "indices": { "client_ind_2": { "index": { "primary_size": "2.5mb", "primary_size_in_bytes": 2710326, "size": "2.5mb", "size_in_bytes": 2710326 } } } }""" b = json.loads(a) print(b['ok']) print(b['indices']['client_ind_2']['index']) 

This will take json as a python dictionary and print “ok” and the index value you want:

 True {u'primary_size': u'2.5mb', u'primary_size_in_bytes': 2710326, u'size_in_bytes': 2710326, u'size': u'2.5mb'} 
+12
source share
 import json dct = json.loads(my_json_str) is_ok = dct['ok'] client_index = dct['indices']['client_ind_2']['index'] 
+1
source share

All Articles