2

First of all, I would like to ask what is a typical format of JSON file with multiple objects?

Is it a list of objects like: [ {...}, {...} ... ]?

Second, I tried to store multiple dict to a single JSON file using python.

I have two JSONs:

test_data = {'profile_img': 'https://fmdataba.com/images/p/4592.png',
 'name': 'Son Heung-Min ',
 'birth_date': '8/7/1992',
 'nation': 'South Korea KOR',
 'position': 'M (R), AM (RL), ST (C)',
 'foot': 'Either'}

and

test_data2 = {'profile_img': 'https://fmdataba.com/images/p/1103.png',
 'name': 'Marc-André ter Stegen ',
 'birth_date': '30/4/1992',
 'nation': 'Germany',
 'position': 'GK',
 'foot': 'Either'}

then I did

with open('data.json', 'w') as json_file:  
    json.dump(test_data, json_file, indent=2)
    json.dump(test_data2, json_file, indent=2)

Of course, I would have iterated a list of dicts to store multiple dicts, but I just did this for now to test if the format is correct. The result .json file looks like

data.json

{
  "profile_img": "https://fmdataba.com/images/p/4592.png",
  "name": "Son Heung-Min ",
  "birth_date": "8/7/1992",
  "nation": "South Korea KOR",
  "position": "M (R), AM (RL), ST (C)",
  "foot": "Either"
}{
  "profile_img": "https://fmdataba.com/images/p/1103.png",
  "name": "Marc-Andr\u00e9 ter Stegen ",
  "birth_date": "30/4/1992",
  "nation": "Germany",
  "position": "GK",
  "foot": "Either"
}

It seems pretty weird because there is not , between two objects. What is the typical way of doing this?

Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • A list of JSON objects might also be saved as JSON Lines where every line is one object. This way the parser does not need to keep state through the full document. The format you provided without any devider between the objects is hard to parse. – Klaus D. Jun 26 '19 at 00:44
  • Not duplicate but perhaps related? https://stackoverflow.com/questions/15487900/how-to-store-multiple-records-in-json – Aurgho Bhattacharjee Jun 26 '19 at 00:46
  • According to the JSON [specification](http://json.org) you need to put each JSON **object** into a JSON **array** with commas between them. However there is another semi-standard format called [JSON Lines](http://jsonlines.org/) where each JSON object is on a line by itself. The motivation for the latter are the many Unixy line-oriented command=line tools — but it's not strictly in JSON format. – martineau Jun 26 '19 at 00:50

1 Answers1

0

You need create an object hold all your date in list first.Then dump this list to file.

test_data_list = [test_data, test_data2]

json.dump(test_data_list, json_file)
todaynowork
  • 976
  • 8
  • 12