Prashant Prashant - 1 year ago 55
JSON Question

Format JSON correctly

I have this JSON, which when fed into online JSON formatter websites, says JSON data not accurate.

{
"Hospitals":[
{
"name":"Centre"
"url":"www.example.com"
},
{
"name":"jdbcd"
"url":"www.example.net"
}
]
}


I know the Correct JSON data is by putting comma at the end of name attribute

{
"Hospitals":[
{
"name":"Centre",
"url":"www.example.com"
},
{
"name":"jdbcd",
"url":"www.example.net"
}
]
}


But in actual I have a very huge data to format. How can i do this programmatically (because python's
json.load(file.json)
gives error) or any online resource(I checked top google results, none worked)

Answer Source

There are supersets of json, such as hjson, that are less strict. I think hjson will work for you:

>>> import hjson
>>> hjson.loads("""  {
    "Hospitals":[
    {
    "name":"Centre"
    "url":"www.example.com"
    },
    {
    "name":"jdbcd"
    "url":"www.example.net"
    }
   ]
  }""")
OrderedDict([('Hospitals', [OrderedDict([('name', 'Centre'), ('url', 'www.example.com')]), OrderedDict([('name', 'jdbcd'), ('url', 'www.example.net')])])])