1
 while True:
        my_dict={}

         b = box.tolist() 
         t = np.array(timestamp).tolist() 
         my_dict["coordinates"] = b
         my_dict["timestamp"]= t
         all_dict.append(my_dict)

  for my_dict in all_dict:    
        with open("co.json", 'a') as fp: 
        json.dump(my_dict,fp)

Output needs to be as json format, but it is not like this { {},{},{} }, it has just dump as {}{}{} without comma separator and without outer {}

2 Answers 2

3

that's because you're dumping each sub-directory as if it was separate single dictionaries. So it doesn't write the wrapping braces nor the commas.

Instead, don't loop on the sub-dicts, just dump the whole list of dicts (adding indent parameter allows to "prettyprint" the dump if needed):

with open("co.json", 'w') as fp:  
    json.dump(all_dict,fp,indent=2)

(you don't need append mode either now, just open for writing/truncating) Note that you won't get a "dict" of dicts but a list of dicts as a result: like [ {a:b}, {c:d} ], which is also valid json.

Sign up to request clarification or add additional context in comments.

Comments

2
with open("coordinates.json", 'w') as fp: 
    fp.write('{')
    for my_dict in all_dict:
        if (my_dict!=all_dict[-1]):
            #fp.write('"key":'+json.dumps(my_dict)+',\n')
        else:
            fp.write('"key":'+json.dumps(my_dict)) 
    fp.write('}')  

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.