0

I have a list of json objects that I would like to write to a json file. Example of my data is as follows:

    {
    "_id": "abc",
    "resolved": false,
    "timestamp": "2017-04-18T04:57:41 366000",
    "timestamp_utc": {
        "$date": 1492509461366
    },
    "sessionID": "abc",
    "resHeight": 768,

    "time_bucket": ["2017-year", "2017-04-month", "2017-16-week", "2017-04-18-day", "2017-04-18 16-hour"],
    "referrer": "Standalone",
    "g_event_id": "abc",

    "user_agent": "abc"
    "_id": "abc",
} {
    "_id": "abc",
    "resolved": false,
    "timestamp": "2017-04-18T04:57:41 366000",
    "timestamp_utc": {
        "$date": 1492509461366
    },
    "sessionID": "abc",
    "resHeight": 768,

    "time_bucket": ["2017-year", "2017-04-month", "2017-16-week", "2017-04-18-day", "2017-04-18 16-hour"],
    "referrer": "Standalone",
    "g_event_id": "abc",

    "user_agent": "abc"
}

I would like to wirte this to a json file. Here's the code that I am using for this purpose:

with open("filename", 'w') as outfile1:
    for row in data:
        outfile1.write(json.dumps(row))

But this gives me a file with only 1 long row of data. I would like to have a row for each json object in my original data. I know there are some other StackOverflow questions that are trying to address somewhat similar situation (by externally inserting '\n' etc.), but it hasn't worked in my case for some reason. I believe there has to be a pythonic way to do this.

How do I achieve this?

Patthebug
  • 4,647
  • 11
  • 50
  • 91
  • have you tried https://stackoverflow.com/questions/252703/append-vs-extend?answertab=votes#tab-top ? – EnriqueDev Jul 03 '17 at 19:42
  • I'm trying to write this data to a file, not read it in. Reading in is totally fine, I am able to read multiple `json` objects in, using the following code: `data = [] with open('filename') as f: for line in f: data.append(json.loads(line))` – Patthebug Jul 03 '17 at 19:47
  • 1
    You can specify option of display within the `dumps` method, eg. can you try `json.dumps(row, indent=4, separators=(',', ': '))` to see how it goes? I will do a proper response if it works. – Kruupös Jul 03 '17 at 19:51
  • 1
    What kind of python type is your original data? Is it a string, list of dictionaries, files? – Håken Lid Jul 03 '17 at 19:54
  • @HåkenLid is right, we need a bit more information about the raw data and it would be helpful to have an example of the poorly formatted data that you are ending up with. – Scot Matson Jul 03 '17 at 20:01
  • The original data is a flat file containing multiple json objects. I only need to strip a string in each json object, and write it back in the exact same format. That is, a flat file containing multiple json (dict) objects. – Patthebug Jul 03 '17 at 20:02
  • @MaxChrétien: your code writes nothing to the output file. Here's what I'm using: `with open("filename", 'w') as outfile1: for row in data: json.dumps(row, indent=4, separators=(',', ': '))` – Patthebug Jul 03 '17 at 20:05
  • @Patthebug does it write something without the optional parameters? this is really weird since it is just supposed to deal with the display; not the content. – Kruupös Jul 03 '17 at 20:07
  • Writing `outfile1.write('\n')` at the end of your loop should work. Can you add some detail to "it hasn't worked in my case" ? – emulbreh Jul 03 '17 at 20:07
  • 1
    The data in your example is not valid json. Multiple objects must be inside either an array or another object. – Håken Lid Jul 03 '17 at 20:08
  • 1
    @Patthebug the code in your comment is missing a `write()` call, that's why there is no output. – emulbreh Jul 03 '17 at 20:09
  • 1
    If you want to get help with this, include a [mcve]. This question is incomplete, since there's no way to tell what variable `data` is. The example "json" is not valid json, so presumably your actual input data is different. – Håken Lid Jul 03 '17 at 20:15

2 Answers2

3

The format of the file you are trying to create is called JSON lines.

It seems, you are asking why the jsons are not separated with a newline. Because write method does not append the newline.

If you want implicit newlines you should better use print function:

with open("filename", 'w') as outfile1:
    for row in data:
       print(json.dumps(row), file=outfile1)
newtover
  • 31,286
  • 11
  • 84
  • 89
0

Use the indent argument to output json with extra whitespace. The default is to not output linebreaks or extra spaces.

with open('filename.json', 'w') as outfile1:
     json.dump(data, outfile1, indent=4)

https://docs.python.org/3/library/json.html#basic-usage

Håken Lid
  • 22,318
  • 9
  • 52
  • 67