简体   繁体   中英

Write multiple lists to a JSON file in python

Assume I have the following lists

list1 = [{"created_at": "2012-01-31T10:00:04Z"},{"created_at": "2013-01-31T10:00:04Z"}] 
list2 = [{"created_at": "2014-01-31T10:00:04Z"}] 

I can write the first list to a JSON file using json.dump(list1,file,indent=2) and the result is

[
  {
    "created_at": "2012-01-31T10:00:04Z"
  },
  {
    "created_at": "2013-01-31T10:00:04Z"
  }
]

My question is, how do I append the contents of the second list? if I simple do json.dump(list2,file,indent=2) , it results in an invalid JSON file as below.

[
  {
    "created_at": "2012-01-31T10:00:04Z"
  },
  {
    "created_at": "2013-01-31T10:00:04Z"
  }
][
  {
    "created_at": "2014-01-31T10:00:04Z"
  }
]

Edit : The lists are created dynamically by parsing about 8000 files. The above lists are just example. I could potentially be writing 8000 lists to the JSON file, so simple appending will not work.

In [1]: import json

In [2]: list1 = [{"created_at": "2012-01-31T10:00:04Z"},{"created_at": "2013-01-31T10:00:04Z"}] 

In [3]: list2 = [{"created_at": "2014-01-31T10:00:04Z"}] 

In [4]: list1.extend(list2)

In [5]: json.dumps(list1)
Out[5]: '[{"created_at": "2012-01-31T10:00:04Z"}, {"created_at": "2013-01-31T10:00:04Z"}, {"created_at": "2014-01-31T10:00:04Z"}]'

or

In [8]: json.dumps(list1 + list2)
Out[8]: '[{"created_at": "2012-01-31T10:00:04Z"}, {"created_at": "2013-01-31T10:00:04Z"}, {"created_at": "2014-01-31T10:00:04Z"}]'

When parse the files append (or extend) to a unique list and finally convert to JSON. Assume that your function for parse is parse .

>>> import json
>>> result = []
>>> for file in files:
...     result.append(parse(file))
...
>>> json.dump(result, file1, indent=2)

I found a little lacking in the explanation given below that's why trying to make a point considered over here. A Json file can have a single parent element. Therefore, if at the first iteration, you dump 1st list then at the 2nd iteration, you will get the formatting error in the file. B/c Json demands these two lists to be wrapped inside one list/array before dumping.

Therefore, you store all lists in one list (either using appending or any other above-mentioned methods). And then you dump this aggregated list into your Json file. However, if you do not want to do so, you will have to create different files for your different lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM