2

I have a Python-script that makes an File with invalid JSON. Now I want to manipulate this JSON-File so it becomes a valid JSON-file by adding a comma between every object, at the beginning of the File a '[' and at the end a ']'. Is there a way to make this with JSON alone or do i have to find a way with other read and write functions?

Exsample_File.json:

    {
    "firstName": "Bidhan",
    "lastName": "Chatterjee",
    "age": 40,
    "email":"bidhan@example.com"
    }
    {
    "firstName": "hanbid",
    "lastName": "jeeChatter",
    "age": 10,
    "email":"example@bidhan.com"
    }
     .... 
    n times

New_File.json:

    [
    {
    "firstName": "Bidhan",
    "lastName": "Chatterjee",
    "age": 40,
    "email":"bidhan@example.com"
    },
    {
    "firstName": "hanbid",
    "lastName": "jeeChatter",
    "age": 10,
    "email":"example@bidhan.com"
    },
     .... 
    n times
    ]

This is the function that makes this JSON-File. I dont want to touch the other code where the str is generated.

data = json.loads(str)
    with open('Example_File.json','ab')as outfile:
        json.dump(data, outfile, indent=2)

So far i dont have an idea to solve this problem. so there is no code sample that would help.

The result should be like the New-File

Daniel S.
  • 33
  • 1
  • 8
  • in this case example_file.json does not appear to be a valid json format. Can you redefine with correct values? – Swadhikar Dec 06 '17 at 13:27
  • Your file appears to contain a sequence of valid JSON snippets. I would open the file, and repeatedly parse JSON structures until you reach end-of-file. For each data being read, write it back and print commas "," where needed (wrap with square brackets) – coredump Dec 06 '17 at 13:29
  • What's the code you used to generate those files? Probably easier to edit that first. If you don't want to edit that, then you could use some regex to find each "}" and place a comma after it. – George Dec 06 '17 at 13:29
  • Do each of the JSON objects in the 1st file represent 1 line in the file? – O.Suleiman Dec 06 '17 at 13:31
  • Don't try to fiddle with the generated file. Generate it correctly in the first place. It would be helpful if you showed the code which you use for generating the file. – mkrieger1 Dec 06 '17 at 13:32
  • I added a code sample but i can´t and don´t want to edit the first script – Daniel S. Dec 06 '17 at 13:39

4 Answers4

2

You may have to read the content as string, manipulate it and load as JSON. Something like this,

import json

with open('Example.json','r') as f:
  data = f.read()

data = "[" + data.replace("}", "},", data.count("}")-1) + "]"
json_data = json.loads(data)

It seems your data has numbers begins with 0, so you may ended up with an exception "ValueError". You may refer how to deal the issue from Why is JSON invalid if an integer begins with 0

Note: I manually removed 0 from "Example.json"

  • 1
    Thats it! it workes super fine. i just added: with open('DumpedText.json', 'w') as out_file: json.dump(json_data,out_file,indent=2) and its Perfect. thx! – Daniel S. Dec 06 '17 at 14:09
0

Can't you do

words.replace('}','},')

This should replace all instances of '}' with a '},'

  • Question is about adding character, how `replace` will add new character ?? – Sanket Dec 06 '17 at 13:39
  • this will add commas. Notice you will have a comma at the end that you should remove and you still need to add square brackets – AndreyF Dec 06 '17 at 13:43
0

First of all I don't think there is a way to parse it directly as JSON array. However, if your JSON objects are not nested a simple way to parse them is to split your string:

with open(YOUR_FILE) as jsons_file:
    jsons = [x.strip() + '}' for x in jsons_file.read().split('}')][:-1]

now you can dump it to file or string using json's library dump or dumps

json.dumps(jsons)

or

with open(OUT_FILE, 'w') as out_file:
    json.dump(jsons, out_file)
AndreyF
  • 1,798
  • 1
  • 14
  • 25
  • I testet youre example on my file and there was an AttributeError: 'list' object has no attribute 'split' – Daniel S. Dec 06 '17 at 13:55
-1

to add automatic comma between each object and add brackets in file to make it complete json just write a simple jq query

jq -s '.' file_name