0

I need to open a json file and add content to it and close it without modifying the previous content. I am loading a file from a .json extension using json.loads() and with open() but when I do that and reprint to next json file including the content that I got from the first file, all floats ARE cut the empty decimal places.

E.G.:

Original json file:

{'lat' : 3.546700,
 'lon' : 6.000000}

After loading the file on python and reprinting adding altitude the new json file looks like this:

{'lat' : 3.5467,
 'lon' : 6.0
 'alt' : 3.467878}

The ideal output would be this;

{'lat' : 3.546700,
 'lon' : 6.000000
 'alt' : 3.467878}

P.D. this is recursive it means that I can't go field by field doing this '{:10.6f}'.format(a), this script is processing more than 100 json files.

Aven Desta
  • 2,114
  • 12
  • 27
  • 2
    The program that generated the geographic point set clearly forced 6 fixed decimal places on both latitude and longitude data. If you wish to use `python` and its `json` library, you must store the numbers in the way you proposed, because loading floats with `json.load{s}`, the information about how the number was represented, is lost. – Captain Trojan Feb 18 '21 at 09:35
  • Could you add the code where you are reading the json into python and adding the altitude? – Ishwar Venugopal Feb 18 '21 at 09:45
  • You can tweak the JSONEncoder and JSONDecoder. See [json package docs](https://docs.python.org/3/library/json.html) and this StackOverflow [answer](https://stackoverflow.com/a/11673759/6018688). – fabianegli Feb 18 '21 at 10:18

0 Answers0