1

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"}
3
  • I think you should do like this out_file.write("\\n") Commented Jul 26, 2017 at 6:39
  • Can you please provide an minimal reproducible example with some of your data? Commented Jul 26, 2017 at 6:41
  • I have added an example Commented Jul 26, 2017 at 6:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
with open("some_file.json", "w") as f: 
    f.write( 
        json.dumps(array_of_dictionaries).replace("},", "},\n")
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.