Ask Your Question
3

How does the function json.load() differ from the function json.loads()?

asked 2021-09-26 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-05-20 20:00:00 +0000

woof gravatar image

The function json.load() reads the JSON data from a file and returns a Python object while the function json.loads() reads JSON data from a string and returns a Python object.

In other words, json.load() is used when you want to read data from a file (using a file object), whereas json.loads() is used when you want to parse a JSON-formatted string.

For example:

import json

# Load JSON data from a file
with open('data.json') as f:
    data = json.load(f)

# Parse JSON-formatted string
json_string = '{"name": "John", "age": 30}'
data = json.loads(json_string)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-26 11:00:00 +0000

Seen: 11 times

Last updated: May 20 '22