1
{"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.


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 it fails.

7
  • 1
    what do you have so far, what's the problem with what you have? Commented Aug 27, 2010 at 18:23
  • Please give an example of what you are trying to do or get a colleague who knows English better to help you write your question. I can't even make a good guess at what you are asking. Commented Aug 27, 2010 at 18:24
  • i want to store the values in json format in python.... Commented Aug 27, 2010 at 18:28
  • JSON is just a string. Python can handle strings just fine. Commented Aug 27, 2010 at 18:29
  • yeah i know...but the problem is that filesname numbers are dictionary object and required items is list when i try to append in list it gives an error TypeError: 'builtin_function_or_method' object is unsubscriptable Commented Aug 27, 2010 at 18:34

1 Answer 1

5
# 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'))
Sign up to request clarification or add additional context in comments.

Comments

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.