Access a particular field in arbitrarily nested JSON data

{
  "status": "200",
  "msg": "",
  "data": {
    "time": "1515580011",
    "video_info": [
      {
          "announcement": "{"announcement_id":"6","name":"INS\u8d26\u53f7","icon":"http:\/\/liveme.cms.ksmobile.net\/live\/announcement\/2017-08-18_19:44:54\/ins.png","icon_new":"http:\/\/liveme.cms.ksmobile.net\/live\/announcement\/2017-10-20_22:24:38\/4.png","videoid":"15154610218328614178","content":"FOLLOW ME PLEASE","x_coordinate":"0.22","y_coordinate":"0.23"}",
          "announcement_shop": "",

How do I grab the content “FOLLOW ME PLEASE” from this json?

replay_data = raw_replay_data['data']['video_info'][0]
announcement = replay_data['announcement']

This grab the everything withing ['announcement'] and I can’t do ['announcement']['content'].

What is the right way to do this?

Thank you in advance for helping me figuring this.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In a single line –

>>> json.loads(data['data']['video_info'][0]['announcement'])['content']
'FOLLOW ME PLEASE'

To help you understand how to access data (so you don’t have to ask again), you’ll need to stare at your data.

First, let’s lay out your data nicely. You can either use json.dumps(data, indent=4), or you can use an online tool like JSONLint.com.

{
    'data': {
        'time': '1515580011',
        'video_info': [{
            'announcement': (    # ***
            """{
                "announcement_id": "6",
                "name": "INS\u8d26\u53f7",
                "icon": "http:\\/\\/liveme.cms.ksmobile.net\\/live\\/announcement\\/2017-08-18_19:44:54\\/ins.png",
                "icon_new": "http:\\/\\/liveme.cms.ksmobile.net\\/live\\/announcement\\/2017-10-20_22:24:38\\/4.png",
                "videoid": "15154610218328614178",
                "content": "FOLLOW ME PLEASE",
                "x_coordinate": "0.22",
                "y_coordinate": "0.23"
            }"""),
            'announcement_shop': ''
        }]
    },
    'msg': '',
    'status': '200'
}

*** Note that the data in the announcement key is actually more json data, which I’ve laid out on separate lines.

First, find out where your data resides. You’re looking for the data in the content key, which is accessed by the announcement key, which is part of a dictionary inside a list of dicts, which can be accessed by the video_info key, which is in turn accessed by data.

So, in summary, “descend” the ladder that is “data” using the following “rungs” –

  1. data, a dictionary
  2. video_info, a list of dicts
  3. announcement, a dict in the first dict of the list of dicts
  4. content residing as part of json data.

First,

i = data['data']

Next,

j = i['video_info']

Next,

k = j[0] # since this is a list

If you only want the first element, this suffices. Otherwise, you’d need to iterate:

for k in j:
    ...

Next,

l = k['announcement']

Now, l is JSON data. Load it –

import json
m = json.loads(l)

Lastly,

content = m['content']
print(content)
'FOLLOW ME PLEASE'

This should hopefully serve as a guide should you have future queries of this nature.

Method 2

You have nested JSON data; the string associated with the 'annoucement' key is itself another, separate, embedded JSON document.

You’ll have to decode that string first:

import json

replay_data = raw_replay_data['data']['video_info'][0]
announcement = json.loads(replay_data['announcement'])
print(announcement['content'])

then handle the resulting dictionary from there.

Method 3

The content of "announcement" is another JSON string. Decode it and then access its contents as you were doing with the outer objects.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x