daniel-s
6/25/2015 - 9:23 AM

Python JSON management.

Python JSON management.

#!/usr/bin/python
 
import json

json_document = {}
 
# Load json from file
with open("model.json") as json_file_r:
  json_document = json.load(json_file_r)
  
# Load json from str
json_document = json.loads(json_formated_str)

# Load multiple json documents (not nested inside a list) -> load line by line
json_documents = []
with open('model.json') as multiple_json_file_r:
  for line in multiple_json_file_r:
    json_documents.append(json.loads(line))

# Save json directly to file
with open('model2.json', 'w') as json_file_w:
  json.dump(json_document, json_file_w)
  
# Save json to string
json_formated_str = json.dumps(json_document)