0
    this is my code:
        import json
        from pprint import pprint

        with open('unAnsQuestions.json') as data_file:    
            data = json.load(data_file)
        print(len(data))
        json_len = len(data)
        json_len+=1
        with open('unAnsQuestions.json', "a") as json_file:
            json.dump({'id':json_len,'fileName':'yogesh'},json_file,indent=2)
            #json_file.write("{}\n".format(json.dumps(json_len)))


                       [{
                      "id":1,
                      "text":"hey?",
                      "answer":"hi" 
                       },
                       {
                      "id":2,
                      "text":"bye.?",
                      "answer":"see you"   
                    }]
  • this is my code which append two values in json file.first is id and another is filename. and next is the json structure in which I want append data - I want append value in json file. - so I have Question.json which contain some data:
    but when I am going to append some value I get this output which is not on proper format.I want this value in bracket '[]'.

                  [{
                      "id":1,
                      "text":"hey?",
                      "answer":"hi" 
                       },
                       {
                      "id":2,
                      "text":"bye.?",
                      "answer":"see you"   
                    }]
                     {
                     "id": 3, 
                     "text":"bye.?",
                     "answer":"see you"
                      }
    
        so, the  "]" is not well formatted. I tried many times but I didn't get output.can you please tell me the exact way?
    
1
  • Please post your code also Commented Sep 30, 2016 at 6:18

2 Answers 2

1

The data is stored as an array of objects in file. The data needs to be appended in the array before dumping. After appending, you need to rewrite the file i.e dump the data variable.

import json from pprint 
import pprint
with open('unAnsQuestions.json') as data_file: 
  data = json.load(data_file)
  print(len(data))
  json_len = len(data)
  json_len+=1 
  with open('unAnsQuestions.json', "w") as json_file: //  write the file not append to it
    data.append({'id':json_len,'fileName':'yogesh'}) // add data in array at last index
    json.dump(data,json_file,inde‌​nt=2) // dump the array
    #json_file.write("{}\n".format(json.dumps(json_len)))
Sign up to request clarification or add additional context in comments.

Comments

0

You should find and read a basic Python tutorial; you seem to have some misconceptions about how things work (for example, you add 1 to json_len, but it's not clear you understand that doing so has no effect on data).

To append an element to the data you've already read, simply use append():

data.append({'id':3, 'text': 'buh-bye', 'answer':'huh?'})

After that, json_file.write(json.dumps(data)) will a new element to the file.

2 Comments

ya I used it, but How should I increament id then.
and every time I don't want enter new id, so using this id+ no need to write id every time

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.