formating my json output file using python

43 Views Asked by At
with open(output_file_path_final, 'w') as f:
        json.dump(final, f, separators=(',', ':'))

this is the key lines of my python code and I get output as below,

{"frame.number":[80.0,87.0,93.0,95.0,99.0,....],"frame.timestamp [s]":[1324055659.394, 1324055659.488, 1324055659.607, 1324055659.613, 1324055659.695,.....], ......... "{31} EVSE Maximum Power Limit [W]":[null, null, null, null, null, null,....]}

separators=(',\n', ':')) : this solution didn't work because it gives every parameter in new line.

{"frame.number":[80.0, 87.0, 93.0, 95.0, 99.0, 103.0, 106.0,.......],
  "frame.timestamp [s]":[1324055659.394, 1324055659.488, 1324055659.607, 1324055659.613, 1324055659.695,.....],
.
.
.
.
  "{31} EVSE Maximum Power Limit [W]":[null, null, null, null, null, null,....]
}
1

There are 1 best solutions below

5
RivenSkaye On

If you check the docs for json.dump, you can read:

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

so consider json.dump(final, f, indent=4) instead