2

I am writing a script that converts from json to ndjson in Python and am pretty new to the language. The cloud environment we use doesn't make use of files, but rather input and output variables. I found this code elsewhere on the site in discussion of conversion and was wondering how to go about converting this from reading and writing files to instead using an input string variable that has json code within it and then saving the ndjson to a string variable?

The code in question:

import json

with open("results-20190312-113458.json", "r") as read_file:
    data = json.load(read_file)
result = [json.dumps(record) for record in data]
with open('nd-proceesed.json', 'w') as obj:
    for i in result:
        obj.write(i+'\n')

1 Answers1

0

Using input methods (assuming you have no newlines) :

import json

# taking input as usual json
input_data = input()

data = json.load(input_data)
result = [json.dumps(record) for record in data]

# save ndjson as a string for later use
ndjson = "\n".join(record)

Since a JSON file might be very long you can use generators for storing result

result = (json.dumps(record) for record in data)

Learn more about it here: Generator expressions vs. list comprehensions


Alternatively, you can use jq if you must not use python

$ echo '[{"foo":0}, {"bar":1}]' | jq -c '.[]'

outputs:

{"foo":0}
{"bar":1}

Learn more about it here: https://stedolan.github.io/jq/

ificiana
  • 98
  • 7