My query is related to when to use json loads and dumps ?
When to convert into list or string?
What is jprint ?
Below is text i was trying to makes sense of:
The function will use the json.dumps() function (“dump string”) which does the opposite of the json.loads() function — it takes a JSON object and returns a string version of it. The json.dumps() function accepts arguments that can specify formatting for the string, which we’ll use to make things easier to read:
create a formatted string of the Python JSON object
It’s easier to understand them if you first look at what load() and dump() do.
If you have a file containing data stored as JSON, and you need to load the contents of that file into python, you can use load(). Simple enough.
However, sometimes you don’t have data available in files. While you will learn about them later, you might have come across APIs. If not, nothing to worry about.
You might want to access data from a particular website. We can use certain libraries to access that data. Of course, if we are accessing the data in Python that means it’s being stored in some variable.
Usually, that data might be stored as a string in that variable but as JSON format (similar to the example in the classroom).
So, you use loads() to convert that data into a Python object so that we can manipulate it much more conveniently. It’s easier to access items from lists and dictionaries than it is from strings.
Now, after we manipulate that data, we need to either - store it in a file or we need to send it back to that website. Of course, we can’t directly store python dictionaries or lists. That’s why we have the JSON format.
So, we use dump(), if we want to convert the Python object back into that string and store it in a file. Or we use dumps() if we just want to convert it into that string and then send the data to that website.
dump() and dump(s) also allow us to “beautify” the output so that it’s more readable.
Taking a more concrete example. I got data from a website using a library. The data I received looks like this when I print it -