BuddyPal
BuddyPal

Reputation: 51

How can I export a dictionary inside a list that's already in a JSON file? | Python

I want to get the dictionary below inside my data array, couldn't find much on this as most tutorials show you how to chuck variable right into the json, without ordering it into an array or object.

the JSON:

#JSON file

{
    "data" : 
    [
      
    
    ]

}


the python file I want to export from:

import json 

#Python file

your_facebook_info = {'name':'johhny', 'age':213, 'money':'lots'}


desired outcome:

{
    "data" : 
    [
      your_facebook_info = {
       'name':'johhny', 
       'age':213, 
       'money':'lots'
      }
    
    ]

}

Upvotes: 0

Views: 47

Answers (1)

Buddy Bob
Buddy Bob

Reputation: 5889

Try this. You read the file, append the new data, write that databack to the file.

import json 

#Python file

your_facebook_info = {'name':'johhny', 'age':213, 'money':'lots'}
with open('text.json','r') as inputfile:
  data = json.load(inputfile)
  data['data'].append(your_facebook_info)

with open("text.json", "w") as outfile: 
    json.dump(data, outfile)

Upvotes: 2

Related Questions