0

This is the structure of my JSON:

"docs": [
        {
            "key": [
                null,
                null,
                "some_name",
                "12345567",
                "test_name"
            ],
            "value": {
                "lat": "29.538208354844658",
                "long": "71.98762580927113"
            }
        },

I want to add the keys to the key list. This is what I want the output to look like:

"docs": [
            {
                "key": [
                    "key1":null,
                    "key2":null,
                    "key3":"some_name",
                    "key4":"12345567",
                    "key5":"test_name"
                ],
                "value": {
                    "lat": "29.538208354844658",
                    "long": "71.98762580927113"
                }
            },

What's a good way to do it. I tried this but doesn't work:

for item in data['docs']:
    item['test'] = data['docs'][3]['key'][0]

UPDATE 1

Based on the answer below, I have tweaked the code to this:

for number, item in enumerate(data['docs']):
    # pprint (item)
    # print item['key'][4]
    newdict["key1"] = item['key'][0]
    newdict["yek1"] = item['key'][1]
    newdict["key2"] = item['key'][2]
    newdict["yek2"] = item['key'][3]
    newdict["key3"] = item['key'][4]
    newdict["latitude"] = item['value']['lat']
    newdict["longitude"] = item['value']['long']

This creates the JSON I am looking for (and I can eliminate the list I had previously). How does one make this JSON persist outside the for loop? Outside the loop, only the last value from the dictionary is added otherwise.

2
  • 3
    As it stands, I don't believe your desired output is valid json Commented Feb 15, 2016 at 21:16
  • Make sure item is initialized as { } and not [ ] (curly braces and not hard brackets Commented Feb 15, 2016 at 21:20

1 Answer 1

1

In your first block, key is a list, but in your second block it's a dict. You need to completely replace the key item.

newdict = {}
for number,item in enumerate(data['docs']['key']):
    newdict['key%d' % (number+1)] = item
data['docs']['key'] = newdict
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work syntactically, % formatting takes precedence over addition so you need to use parentheses: 'key%d' % (number+1). Or use the new way: 'key{}'.format(number+1).
Doesn't work. I get this error: list indices must be integers, not str in the for statement. 'docs' is a list. To get to a key, we need something like this: data['docs'][3]['key']
You have the order wrong. It should be data['docs']['key'][3].
To drill inside the key, we do need the [3] after ['key']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.