0

I am trying to write a JSON file in my python script I have the following:

#write JSON file:
data = {
   'description' : 'ACME',
   'taskType' : 100,
   'scanFileTypes' : 542.23,
   'connections' : connectionName,
   'fileTypeExclude' : False,
   'policies': 'PCI_Hadoop'
}



with open(jsonPropPath, 'w') as outfile:
    json.dump(data, outfile, ensure_ascii=False)

My JSON file looks like:

cat /home/matt/DGagent/jsonFile_20150805102636.json

{"description": "ACME", "scanFileTypes": 542.23000000000002, "connections": "dgcl_20150805102636", "policies": "PCI_Hadoop", "taskType": 100, "fileTypeExclude": false}[matt@server1 ~]

The decimal is being extended as you can see, also its adding the server: [matt@server1 ~]

Can anyone help me correct these issues?

theMadKing
  • 2,064
  • 7
  • 32
  • 59
  • 7
    It's not adding the server. That's your shell prompt. You just mis-copied the output because there is no terminal newline in the JSON output. – larsks Aug 05 '15 at 14:36
  • Oh great, any way to keep the decimal from trailing like that? – theMadKing Aug 05 '15 at 14:42
  • 1
    For the decimal, see this answer: http://stackoverflow.com/a/1447581/2744166 – Joe Young Aug 05 '15 at 14:42
  • Are you sure the decimal is like that in the file? Running your code with Python 3.4 and actually opening the file to look at it I'm not getting the extended decimal. – kylieCatt Aug 05 '15 at 14:43
  • That doesn't happen to me on Python2.7. Are you sure there's no other intermediate step that could affect the decimal? – SuperBiasedMan Aug 05 '15 at 14:46
  • The solution provided by jlyoung has worked. Thanks – theMadKing Aug 05 '15 at 14:46
  • possible duplicate of [Format floats with standard json module](http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module) – kylieCatt Aug 05 '15 at 14:48

0 Answers0