Difference between revisions of "Python:JSON"
Jump to navigation
Jump to search
m |
|||
Line 1: | Line 1: | ||
[[Category:Python]] | [[Category:Python]] | ||
− | ;import json | + | ;import [https://docs.python.org/3/library/json.html json] |
:Enable json functions. The json module is standard available (no installation needed) | :Enable json functions. The json module is standard available (no installation needed) | ||
+ | |||
+ | Read a json file and return it as [[Python:DataTypes#Dictionary_or_dict|dict]]. | ||
+ | <syntaxhighlight lang=python> | ||
+ | import json | ||
+ | with open(filename) as fh: | ||
+ | data = json.load(fh) | ||
+ | </syntaxhighlight> | ||
;json.dumps(dict, indent=4) | ;json.dumps(dict, indent=4) | ||
− | : | + | :Convert a dict into a json string nicely structured. Indent each level with 4 spaces. |
− | :Can be used on other datatypes too. | + | :Can be used on other [[Python:DataTypes|datatypes]] too. |
Revision as of 23:14, 21 December 2018
- import json
- Enable json functions. The json module is standard available (no installation needed)
Read a json file and return it as dict.
import json
with open(filename) as fh:
data = json.load(fh)
- json.dumps(dict, indent=4)
- Convert a dict into a json string nicely structured. Indent each level with 4 spaces.
- Can be used on other datatypes too.