The json.dumps() function in Python formats a Python object into a JSON-formatted string, and the indent parameter controls the indentation level for readability.
json.dumps() with indent:
indent=4adds four spaces per nesting level, producing a standard pretty-printed format.indent=2uses two spaces per level, ideal for more compact readability.indent=None(default) produces the most compact output with no extra whitespace.indent=0or negative values insert only newlines, no spaces.
Example:
import json
data = {"name": "Alice", "age": 30, "hobbies": ["reading", "chess"]}
print(json.dumps(data, indent=4))Output:
{
"name": "Alice",
"age": 30,
"hobbies": [
"reading",
"chess"
]
}Key Options:
sort_keys=True: Sorts dictionary keys alphabetically.separators=(',', ': '): Customizes spacing around commas and colons (e.g.,(',', ': ')for readable output).ensure_ascii=False: Allows non-ASCII characters (e.g., emojis) to be preserved.
Use
json.dumps()for string output; usejson.dump()to write directly to a file withindent.
Python 2.7
there is a workaround that can be implemented using regular expressions :
import re
dump = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
#Replaces spaces with tab
new_data = re.sub('\n +', lambda match: '\n' + '\t' * (len(match.group().strip('\n')) / 3), dump)
json.dump(new_data, open('dev_integrated.json', 'w')
Python 3.2+
From the Docs :
If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or "" will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as "\t"), that string is used to indent each level.
Hence the TAB-indentation can be implemented as follows:
json.dump(jString, open('dev_integrated.json', 'w'), sort_keys=True, indent='\t', separators=(',', ': '))
Answer from Stack on Stack Overflowpython - How to prettyprint a JSON file? - Stack Overflow
Will json.dump() get the indent argument?
JSON dumps indent tab
removing new line inside square brackets in json file
Videos
Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:
>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
To parse a file, use json.load():
with open('filename.txt', 'r') as handle:
parsed = json.load(handle)
You can do this on the command line:
python3 -m json.tool some.json
(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).
Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.
You can also use jq:
jq . some.json
and you get colors as a bonus (and way easier extendability).
Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.
We use Python 2.7 and I want to change the indention of JSON.dumps() to TABS instead of SPACES. When you do indent=8, it will insert 8 spaces, but I want to insert 2 tabs. I have read that this is possible in Python 3.3 by doing indent="\t\t" but we use Python 2.7.
I've noticed that using indent in json.dumps, add new lines inside the lists (inside square brackets)
{
"mylist":[
1,
2,
3,
],
"who": "me"
}while i wanted something like this
{
"mylist": [1,2,3]
"who": "me"
}I tried to solve it using regex, but i cant figure it out. I tried to split the problem so
i want to display everything inside the square brackets
\[(.*?)\]i want to capture all new lines
(\r\n|\r|\n)
So i thought that writing something like re.sub("\[(\r\n|\r|\n)\]","", myjson) would have worked but nope.
any ideas?
You need to use the indent argument in json.dumps() to create the pretty effect.
with open('filename.json', 'w') as f:
f.write(json.dumps(data, indent=4)
To get pretty printing using json.dumps() you need to include a parameter like indent=4. See the docs here.
Update, after seeing the image:
The problem you have here is that in your JSON, DbCollectionName is a string that contains more JSON. This is "Nested JSON". You need to call json.loads() on each of those strings to convert them to objects.
Hi,
I would like to read json into Python code, and then output processed json. In order to get started with this, I have written very basic Python, and am attempting to read in very basic json I found online.
The input json is:
{
"firstName": "John",
"lastName": "Doe",
"hobbies": ["biking", "coding", "rapping"],
"age": 35,
"children": [
{
"firstName": "hector",
"age": 6
},
{
"firstName": "cassandra",
"age": 8
}
]
}The code is:
import json
if __name__ == '__main__':
print( "start" )
# read and load input json
json_input_filename = "input.json"
json_input = open( json_input_filename )
json_input_dict = json.load( json_input )
# write output json
json_output_filename = "output.json"
with open( json_output_filename, 'w' ) as json_output:
json.dump( json_string, json_output )
print( f"end" )and the output is:
"{\"firstName\": \"John\", \"lastName\": \"Doe\", \"hobbies\": [\"biking\", \"coding\", \"rapping\"], \"age\": 35, \"children\": [{\"firstName\": \"hector\", \"age\": 6}, {\"firstName\": \"cassandra\", \"age\": 8}]}"What can I do in order to preserve something resembling the original formatting? I'm going to load this output into some other code in order to process it further.
Thank you very much