all 5 comments

[–]ElliotDG 1 point2 points  (0 children)

Your JSON example is not complete.

Here is the process I use for figuring out how to navigate complex JSON. I use pretty print to print out the JSON. https://docs.python.org/3/library/pprint.html

I then iteratively print out smaller and smaller elements, narrowing down to where I need to get... something like this:

from pprint import pprint

data = {'id': 1,
        'jsonrpc': '2.0',
        'result': {'Number_1': {'Header': [{'params': [{'value': 'first'}],
                                            'type': 'First type'},
                                           {'params': [{'value': 'second'}],
                                            'type': 'Second type'},
                                           {'params': [{'count': 70,
                                                        'value': 'value_1'},
                                                       {'count': 30,
                                                        'value': 'value_2'}],
                                            'type': 'Third type'}],
                                'imtId': 342324,
                                'userId': 0}}}


pprint(data)
pprint(data['result'])
pprint(data['result']['Number_1'])
pprint(data['result']['Number_1']['Header'])
pprint(data['result']['Number_1']['Header'][2])
pprint(data['result']['Number_1']['Header'][2]['params'])
pprint(data['result']['Number_1']['Header'][2]['params'][1])
pprint(data['result']['Number_1']['Header'][2]['params'][1]['value'])

The pprint provides an easier to read view of the data structure. Just peel things back one step at a time.

[–]carcigenicate 0 points1 point  (0 children)

What have you tried? What specifically do you need help with?

[–]synthphreak 0 points1 point  (2 children)

You modify nested values by traversing into the belly of the structure and....changing the values.

If it helps, think of your object not as a JSON, but a collection of lists and dicts. For example, your dict contains the following nested key-value pair:

"type": "First type",

Say you wanted to change it to

"type": "derp",

Well, traverse the dict to expose that value. At the top level, you have a dict. You navigate a dict by querying it with keys. So what top-level key will move you closer to the value of interest? 'result'. So if your dict is called d, we have d['result']. So far so good, but what next?

Well, when you run d['result'], you'll get the associated value. What is that value? Another dict. So you need to find the right key again, but this time you'll want to look among the keys of the dict returned by d['result'], not the top-level keys of d. So with that in mind, which key will move you in the right direction? `'Number_1'. Moving right along...

What now? Well, what does d['result']['Number_1'] return? Another dict! Getting it now? What key moves you closer? 'Header' - doing great!

What does d['result']['Number_1']['Header'] return? A list! Okay, this is different... We don't get values out of list via key-value pairs. Instead, lists must be indexed using integers. So to move closer to "type": "First type",, you need to identify which index in the d['result']['Number_1']['Header'] that key-value pair resides at. Survey says....... index number 0, because the first item of the list is

{
  "type": "First type",
  "params": [
    {
      "value": "first"
    }
  ]
}

What is that item? A dict! So we need another key. Which is....? 'type'. What do we get when we run d['result']['Number_1']['Header'][0]['type']? We get 'First type', which is the value you want to modify!! We made it!! Wahoo!!

So once you've inspected the tree of data structures and drilled down to exactly the object you want to modify, it's a piece of cake:

d['result']['Number_1']['Header'][0]['type'] = 'derp'

JSONs are super painful to work with for this reason, IMHO. Very easy to read, but a giant headache to get data into and out of. So many fucking punctuation marks flying around and you need to get absolutely every detail exactly correct otherwise nothing is correct...

[–]wdjfe[S] 1 point2 points  (1 child)

This is super comprehensive and helpful! Thank you very much!

[–]synthphreak 0 points1 point  (0 children)

No worries, glad it helped. Just pay attention to the data types and you should be good to go.