0

I have a Json string:

{"spatialReference":{"wkid":102100},"geometryType":"esriGeometryPolygon","features":[{"geometry":{"rings":[[[-13945398.575078921,7313414.186040432],[-13945392.60343609,7313379.550512001],[-13945410.518364588,7313360.142672794],[-13945433.509189496,7313387.313647684],[-13945398.575078921,7313414.186040432]]],"spatialReference":{"wkid":102100}}},{"geometry":{"rings":[[[-13945451.424117994,7313427.025072522],[-13945426.641800238,7313432.399551072],[-13945432.314860929,7313413.588876149],[-13945449.035460861,7313412.991711865],[-13945451.424117994,7313427.025072522]]],"spatialReference":{"wkid":102100}}}]}

How do I save this string as a new .json file using python? I probably need to create a new empty .json file and then populate it with the string.

Thanks!

Olak
  • 15
  • 1
  • 3
  • 1
    Possible duplicate of [How do I write JSON data to a file?](https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file) – Anton vBR Feb 28 '18 at 00:27

2 Answers2

0

If it is already a string just do:

file = open('json.json','w')
file.write('your_json_string')
file.close()
Gustavo Topete
  • 1,246
  • 1
  • 9
  • 15
  • is json.json a new file? Can I choose where it will be stored? – Olak Feb 28 '18 at 00:15
  • Yes, open() will create it if it doesn't exist. And yes, you can specify the directory, just add it as an absolute route, like: '/home/user/json.json' – Gustavo Topete Feb 28 '18 at 00:19
0

This is how you do it. This is a dupe however...

import json

with open('output.json', 'w') as f:
    json.dump({'a':'b'},f,indent=2)  # {'a... being your json string
Anton vBR
  • 18,287
  • 5
  • 40
  • 46