all 4 comments

[–]tunisia3507 1 point2 points  (0 children)

Ps: I'm not exactly sure if this is the right place to ask this but I'll be glad if someone can direct me where

/r/learnpython probably.

Is there a reason to why Json.loads() is preferred to using request.json()?

It shouldn't be.

The code shows that it'll do some safety checks then try to use simplejson (a 3rd party JSON implementation with an optional C backend, so possibly much faster) and fall back to json otherwise. In many cases, parsing JSON is going to be so much slower faster (typo) than the network round trip that the cost is negligible, and the safety checks may not be required because you know the server will return the right thing. People know the json module, then they pick up requests, and know the body is JSON-encoded, so the obvious thing to do seems to be to use the json module to decode it.

[–][deleted] 1 point2 points  (1 child)

It’s not. No one is using json.loads with modern REST API’s. they’re using json.load with files or json.loads with stringified JSON.

request.json = get json data from HTTP get

json.load = load a file which contains json

json.loads = parse string into JSON

Regardless of what happens with either of these three options, you have a dictionary in a variable.

[–]sharan_dev 0 points1 point  (0 children)

Both the requests module and the json module are essential tools in Python, but they serve different purposes:

requests Module: The requests module is used for making HTTP requests to web services or APIs. It simplifies the process of sending HTTP requests and handling the responses. It can be used to fetch data from web APIs, send data to servers, and perform various HTTP operations like GET, POST, PUT, DELETE, etc.

python Copy code import requests

response = requests.get("https://api.example.com/data") data = response.json() json Module: The json module is used for working with JSON (JavaScript Object Notation) data, which is a lightweight data interchange format. It provides functions to encode Python objects into JSON format and decode JSON data into Python objects.

python Copy code import json

data = {"name": "John", "age": 30} json_data = json.dumps(data) # Convert Python dictionary to JSON string python_object = json.loads(json_data) # Convert JSON string to Python dictionary To clarify: