0

I have discovered that mjson.tool converts real number using scientific notation, e.g:

$ echo '{"k":0.000000581}' | python -mjson.tool
{
    "k": 5.81e-07
}

However, I would like to avoid this conversion, so the former get printed as:

{
    "k": 0.000000581
}

It is possible to use mjson.tool in such way, please?

fgalan
  • 11,732
  • 9
  • 46
  • 89

1 Answers1

1

From what I see, mjson module just convert the input to json and back to string with indentation and sorted keys.

This can be done with:

>>> json.dumps(json.loads('{"k":0.000000581}', indent=2, sort_keys=True))
'{"k": 5.81e-07}'

To avoid the scientifique notation, see @Veedrac answer on the subject:
https://stackoverflow.com/a/18936966/956660


Edit: Any tools that only reformat and does not try to parse/cast types will work.

I tried with yajl-tools:

user$ sudo apt-get install yajl-tools
user$ echo '{"a": 0.0000000000000001337}' | json_pp 
{
   "a" : 1.337e-16
}
user$ echo '{"a": 0.0000000000000001337}' | json_reformat
{
    "a": 0.0000000000000001337
}
Community
  • 1
  • 1
Cyrbil
  • 6,341
  • 1
  • 24
  • 40
  • Thanks! I'm trying to solve the problem at "shell space" (maybe some parameter to the mjson.tool invocation?) but in the case it isn't possible your feedback for a "Python space" solution is very valuable. – fgalan Nov 26 '15 at 10:36