Published on

How to Read and Write json data with Python

Authors

This is a short tutorial on how to use python to write and read files.

Example

To write the following json data:

{"name": "ruan"}

To a file named /tmp/data.json, we will be using this code:

import json

data = {"name": "ruan"}
with open('data.json', 'w') as f:
    f.write(json.dumps(data))

When we execute that code, we will find the data inside that file:

$ cat /tmp/data.json
{"name": "ruan"}

And if we want to use python to read the data:

import json

with open('data.json', 'r') as f:
    json.loads(f.read())

When we execute that code, we will see:

{'name': 'ruan'}

Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscrube to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee