Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append data to a json file?

Tags:

python

json

I'm trying to create a function that would add entries to a json file. Eventually, I want a file that looks like

[{"name" = "name1", "url" = "url1"}, {"name" = "name2", "url" = "url2"}] 

etc. This is what I have:

def add(args):     with open(DATA_FILENAME, mode='r', encoding='utf-8') as feedsjson:         feeds = json.load(feedsjson)     with open(DATA_FILENAME, mode='w', encoding='utf-8') as feedsjson:         entry = {}         entry['name'] = args.name         entry['url'] = args.url         json.dump(entry, feedsjson) 

This does create an entry such as {"name"="some name", "url"="some url"}. But, if I use this add function again, with different name and url, the first one gets overwritten. What do I need to do to get a second (third...) entry appended to the first one?

EDIT: The first answers and comments to this question have pointed out the obvious fact that I am not using feeds in the write block. I don't see how to do that, though. For example, the following apparently will not do:

with open(DATA_FILENAME, mode='a+', encoding='utf-8') as feedsjson:     feeds = json.load(feedsjson)     entry = {}     entry['name'] = args.name     entry['url'] = args.url     json.dump(entry, feeds) 
like image 610
Schiphol Avatar asked Oct 21 '12 02:10

Schiphol


People also ask

Which function is used to append data using JSON?

loads() function is present in python built-in 'json' module.

How do I add a json file to another json file?

JSON doesn't have any mechanism to reference/include JSON in other files. You manually have to edit the JSON and insert your other JSON there. Or load both JSON files with whatever language you are processing the data in, and write some custom logic to merge the data in that language.

How do I update an existing json file in Python?

Update & Delete JSON object Updating a JSON object in Python is as simple as using the built-in update() function from the json package we have imported. The update method is used to add a new key-value pair to the JSON string that we declared in our code.


1 Answers

json might not be the best choice for on-disk formats; The trouble it has with appending data is a good example of why this might be. Specifically, json objects have a syntax that means the whole object must be read and parsed in order to understand any part of it.

Fortunately, there are lots of other options. A particularly simple one is CSV; which is supported well by python's standard library. The biggest downside is that it only works well for text; it requires additional action on the part of the programmer to convert the values to numbers or other formats, if needed.

Another option which does not have this limitation is to use a sqlite database, which also has built-in support in python. This would probably be a bigger departure from the code you already have, but it more naturally supports the 'modify a little bit' model you are apparently trying to build.

like image 116
SingleNegationElimination Avatar answered Sep 21 '22 08:09

SingleNegationElimination