Pretty print invalid json with python

There is a way to prettyly print the correct JSON with Python: How can I print JSON in a shell (unix) script?

However, jsonlint.com prints JSON nicely, even if something is wrong.

I want to do this and I am using Python. Does anyone know a script that does this?

Thanks:)

+4
source share
4 answers

There is a built-in way to do this with Python:

python -m json.tool myfile.json 
0
source

You can use the demjson module in either your own script tool or jsonlint.py , which displays

+1
source

@kelw already pointed out demjson , which works well for slightly invalid JSON. For data that matches JSON even less, I wrote barely_json :

 from barely_json import parse from pprint import pprint invalid_json = '[no, , {complete: yes}]' data = parse(invalid_json) pprint(data) 
+1
source

Try the jsbeautifier library to decorate JavaScript. This works because JSON is an "almost" subset of JavaScript.

 import json import jsbeautifier invalid_json = '{"hello": "world",}' json.loads(invalid_json) # ValueError: Expecting property name: line 4 column 1 (char 25) jsbeautifier.beautify(invalid_json) # '{\n "hello": "world",\n}' 
+1
source

All Articles