Skip to content
Advertisement

How to add a newline function to JSON using Python

I have a JSON that I had to manipulate and the code looks like this. I had to manipulate the data because of an invalid part of the data that I could not use when uploading to BigQuery.

import json

with open('firetobq_peripheral.json', 'r') as in_file:
    dicts = []
    for line in in_file.readlines() : 
        d = json.loads(line.strip())
        if d.get('Peripherals'): 
            del d['Peripherals']
        dicts += [d]

with open('firetobq_peripheral5.json', 'w') as out_file:
    out_file.write(json.dumps(dicts))
    out_file.write("n")

However out_file.write("n") is not making the JSON write each set of data on a newline like it has in the previous JSONs I have worked on. Does anybody know why this is happening and how to get around this? Thanks for the help.

I need the data to be presented like this

{"AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "createdAt": "2017-04-05T07:05:30.408Z", "updatedAt": "2017-04-05T07:08:49.569Z",  "connectedPeripheralCount": 1, "iOSVersion": "10.2.1"}
{"objectId": "20H5Hg2INB", "foundedPeripheralCount": 0, "DeviceVendorID": "5B7F085E-B3B6-4270-97DC-F42903CDEAC1", "version": "1.3.5(5801)", "deviceType": "iPhone 6", "createdAt": "2015-11-10T06:16:45.459Z", "updatedAt": "2015-11-10T06:16:45.459Z", "connectedPeripheralCount": 0, "iOSVersion": "9.1"}
{"AppName": "DataWorks", "foundedPeripheralCount": 2, "version": "1.6.2(8069)", "deviceType": "iPhone 6s", "createdAt": "2017-04-12T10:05:05.937Z", "updatedAt": "2017-07-06T07:33:02.006Z", "connectedPeripheralCount": 8, "iOSVersion": "10.2.1"}

Advertisement

Answer

If you wish to write your data exactly as you have read it in, then you will need to iterate over each dictionary in dicts:

with open('firetobq_peripheral5.json', 'w') as out_file:
    for d in dicts:
        out_file.write(json.dumps(d))
        out_file.write("n")

If this is not required, then json.dump would be the best option.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement