parsing json fields in python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2
--
Chapters
00:00 Question
00:57 Accepted answer (Score 20)
01:34 Answer 2 (Score 2)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/1957...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #simplejson
#avk47
    --
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2
--
Chapters
00:00 Question
00:57 Accepted answer (Score 20)
01:34 Answer 2 (Score 2)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/1957...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #simplejson
#avk47
ACCEPTED ANSWER
Score 20
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 python dictionary and will print 'ok' and 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'}
ANSWER 2
Score 2
import json
dct = json.loads(my_json_str)
is_ok = dct['ok']
client_index = dct['indices']['client_ind_2']['index']