0

I have one ndJson file where I have to manipulate the data, I am just extracting the data in one variable in dictionary format and wanted to create new json file to store it, But I am unable to do so.

I have tried creating new file which can store the value which my dictionary variable holds.

Here is the code

with open("C:\\Users\\Lalith K\\Downloads\\ENUMtransactions.json") as f:
    data = [json.loads(l) for l in f.readlines()]

    for i in range(len(data)):
        if i % 2 == 0:
            newDataE = (data[i])
            print(newDataE)
            outfile = open('p.json', 'w')
            print(newDataE, file=outfile)

My newDataE holds the output in dictionary format i.e. {'index': {'_index': 'packets-2022-09-28', '_type': 'pcap_file'}} during my each repetition this is the output with changed date. When I am trying to create new file it is only holding one value instead of all the values.

Please help me how to add all the values in new JSON file where each json is seprated with line break from another.

Image for purpose of output I want which I am getting on the console I want it in the JSONfile. enter image description here

  • 1
    https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file json.dump is your friend. – Alexander Jordan Sep 28 '22 at 11:42
  • @AlexanderJordan It wont help, I have stored my dictionary in newdataE but in the above reference it asks my for the .JSON file as an input, I cant do that because of some manipulations i have to do with these data. – Lalit Kumar Singh Sep 28 '22 at 11:46

1 Answers1

2

If your ENUMtransactions.json is well-formatted, you can simply do as follow:

with open("C:\\Users\\Lalith K\\Downloads\\ENUMtransactions.json") as f:
        data = [json.loads(l) for l in f.readlines()]
        outfile = open('p.json', 'w') # Open your file first
    
        for i in range(len(data)):
            if i % 2 == 0:
                outfile.write(json.dumps(data[i])) # Write the data into your file

        outfile.close() # Don't forget to close your opened file!

You can also replace the second file opening by with open(...), so no need to close it afterwards - as it is automatically closed.

Bloodbee
  • 827
  • 8
  • 23
  • Throwing error "json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 67)" – Lalit Kumar Singh Sep 28 '22 at 11:50
  • As i said, it only works if your base file is well-formatted. If you got this error, it looks like your file ENUMtransactions.json is not really a json, but just a text file containing line by line json dictionnaries. Try reusing your data = [json.loads(l) for l in f.readlines()] instruction instead of json.load(f). – Bloodbee Sep 28 '22 at 11:53
  • Ok, so my basefile is ndJSon I already mentioned, I wanted to store values which I am extracting in newDataE continuously till the loop works, as you are seeing in the image. – Lalit Kumar Singh Sep 28 '22 at 11:56
  • Ok i edited my answer. Also have a look here, it will surely help: https://pypi.org/project/ndjson/ – Bloodbee Sep 28 '22 at 12:03