tags:

views:

71

answers:

2
{"required_items":[
                   {
                    "filename":"abcd",
                    "no":"3"
                   },
                   {
                    "filename":"abc",
                    "no":"2"
                   }
                  ]}

I am not getting the code of the JSON format in Python - I want to insert the filename and no through a loop.

+2  A: 
# data.txt

{"required_items":[
                   {
                    "filename":"abcd",
                    "no":"3"
                   },
                   {
                    "filename":"abc",
                    "no":"2"
                   }
                  ]}

# parser.py

import json 

data = json.load(open('data.txt'))

for file in data:
    print file['filename']

# This will output:
#  abcd
#  abc

If you want to append new items:

data.append({ 'filename': 'foo',
            'nr': 1 })

json.dump(data, open('data.txt', 'w'))
leoluk
A: 
list_of_other_ids={}
for i in xxxx:    
entry={}
entry['filename'] = "XXXX"
entry['no'] =XX
list_of_other_ids.append(entry)

i am doing like this... and its fails

bhaskaragr29
You can't append to a dict.
Ignacio Vazquez-Abrams
You cannot append to dictionary.You should use update..list_of_other_ids.update({key: value})
Hulk