1
votes

I have a huge json array file downloaded which needs to be split into smaller files but I need the smaller files in the below format (newline for each new object in array): (original json is also in the same format)

[
{"a":"a1","b":"b1","c":"c1"},
{"a":"a2","b":"b2","c":"c2"},
{"a":"a3","b":"b3","c":"c3"}
]

I used json.dump but it is just printing the smaller array in a single line and using the indent option is also not giving me the output in the above format

1
are there no quotation marks on purpose? I'm not sure that is valid json... - Tadhg McDonald-Jensen
mind shedding light on why it must be in a specific format instead of just readable json? - heyiamt
@ Tadhg McDonald-Jensen : missed the quotations, added them now - java2890
Need more information. What does the original json look like? - AndrewSmiley
@ AndrewSmiley original json is also in the same format - java2890

1 Answers

2
votes

Although I don't know what your original json looks like, you would basically want something like this

lines = []
for something in original_json:
    line={something['a']:something['aa']} #whatever you need to do to get your  values
    lines.append(line) 
    #alternatively you can simplify this by doing lines.append({something['a']:something['aa'], etc}
with open('myfile.json', 'a+') as f1:
    f1.write("[\n")
    for line in lines:
         f1.write("%s,\n"%(line))
    f1.write("]")