OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Use Python to control json.dump() format with kwarg "default="?

  • Thread starter Thread starter prainbow
  • Start date Start date
P

prainbow

Guest
In Python I have to keep dumping to a storage file an array of user data consisting of JSON objects (simple format: [{}, {}...]). As the program captures and records user data, the array [] will increase potentially into the small thousands of objects. Each object {} consists of up to about fifty key_value pairs. How would I write a custom function for json.dump() using the native "default=" kwarg that will start each object on a new line, and add an extra linespace between? Using the "indent=" kwarg to pretty-print the array makes a file that is too long vertically, and without that kwarg I get a dense mass of objects hard to read.

To illustrate the problem, take a much shorter array. example = [{"a":1, "b":2, "c":3,}, {"d":1, "e":2, "f":3,}, {"g":1, "h":2, "i":3}, {"j":6, "k":7, "l":8}] Without using indent, the output to my .json file is too compact.

Code:
[{"a": 1, "b": 2, "c": 3}, {"d": 1, "e": 2, "f": 3}, {"g": 1, "h": 2, "i": 3}, {"j": 6, "k": 7, "l": 8}]

If I use indent, I get a narrow vertical column.

Code:
[
  {
    "a": 1,
    "b": 2,
    "c": 3
  },
  {
    "d": 1,
    "e": 2,
    "f": 3
  },
  {
    "g": 1,
    "h": 2,
    "i": 3
  },
  {
    "j": 6,
    "k": 7,
    "l": 8
  }
]

To minimize whitespace while maintaining readability, I want output in this format (here arranged manually).

Code:
[
    {"a": 1, "b": 2, "c": 3},

    {"d": 1, "e": 2, "f": 3},

    {"g": 1, "h": 2, "i": 3}, 

    {"j": 6, "k": 7, "l": 8}
]

When each object holds up to fifty key:value pairs, this format, even with wrapping, will achieve both ends. I'm surprised the method provides no such option. But I can't find enough info on the nuts and bolts of JSON code to impose newlines and extra linespaces within the existing json.dump() method. Any help?
<p>In Python I have to keep dumping to a storage file an array of user data consisting of JSON objects (simple format: [{}, {}...]). As the program captures and records user data, the array [] will increase potentially into the small thousands of objects. Each object {} consists of up to about fifty key_value pairs.
How would I write a custom function for json.dump() using the native "default=" kwarg that will start each object on a new line, and add an extra linespace between? Using the "indent=" kwarg to pretty-print the array makes a file that is too long vertically, and without that kwarg I get a dense mass of objects hard to read.</p>
<p>To illustrate the problem, take a much shorter array.
<code>example = [{"a":1, "b":2, "c":3,}, {"d":1, "e":2, "f":3,}, {"g":1, "h":2, "i":3}, {"j":6, "k":7, "l":8}]</code>
Without using indent, the output to my .json file is too compact.</p>
<pre><code>[{"a": 1, "b": 2, "c": 3}, {"d": 1, "e": 2, "f": 3}, {"g": 1, "h": 2, "i": 3}, {"j": 6, "k": 7, "l": 8}]
</code></pre>
<p>If I use indent, I get a narrow vertical column.</p>
<pre><code>[
{
"a": 1,
"b": 2,
"c": 3
},
{
"d": 1,
"e": 2,
"f": 3
},
{
"g": 1,
"h": 2,
"i": 3
},
{
"j": 6,
"k": 7,
"l": 8
}
]
</code></pre>
<p>To minimize whitespace while maintaining readability, I want output in this format (here arranged manually).</p>
<pre><code>[
{"a": 1, "b": 2, "c": 3},

{"d": 1, "e": 2, "f": 3},

{"g": 1, "h": 2, "i": 3},

{"j": 6, "k": 7, "l": 8}
]
</code></pre>
<p>When each object holds up to fifty key:value pairs, this format, even with wrapping, will achieve both ends. I'm surprised the method provides no such option.
But I can't find enough info on the nuts and bolts of JSON code to impose newlines and extra linespaces within the existing json.dump() method. Any help?</p>
 

Latest posts

S
Replies
0
Views
1
Sergey Bakaev Rettley
S
Top