Python - File JSON Format

Introduction

Use the json module to translate Python objects to and from a JSON serialized string representation in memory:

Demo

import json 

name = dict(first='Bob', last='Smith') 
rec  = dict(name1=name, job=['dev', 'mgr'], age=40.5) 
print( json.dumps(rec) )

S = json.dumps(rec) #  w  w w  .j  ava 2  s.  c  om
print( S )

O = json.loads(S) 
print( O )
print( O == rec )

Result

To translate Python objects to and from JSON data strings in files.

Demo

import json 

rec  = dict(name=name, job=['dev', 'mgr'], age=40.5) 
json.dump(rec, fp=open('testjson.txt', 'w'), indent=4) 
print(open('testjson.txt').read()) 
P = json.load(open('testjson.txt')) 
print( P )# from w  ww  .j  a  va  2  s  .co  m

Result