Difference between revisions of "Python:JSON"
Jump to navigation
Jump to search
m |
|||
Line 3: | Line 3: | ||
:Enable json functions. The json module is standard available (no installation needed) | :Enable json functions. The json module is standard available (no installation needed) | ||
− | + | ;json.load(<data>) | |
+ | :Return <data> as [[Python:DataTypes#Dictionary_or_dict|dict]]. | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
import json | import json |
Revision as of 20:38, 9 June 2019
- import json
- Enable json functions. The json module is standard available (no installation needed)
- json.load()
- Return as dict.
import json
with open(filename) as fh:
data = json.load(fh)
Below can be used on datatypes like lists and tuples too, not on sets.
- json.dumps(dict, indent=4)
- Convert a dict into a json string nicely formatted. Indent each level with 4 spaces.
- json.dump(dict, fh, indent=4)
- Dumps to the file opened on filehandle fh.