Screen Link:
List Comprehensions And Lambda Functions | Dataquest

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:

def jprint(obj):

create a formatted string of the Python JSON object

text = json.dumps(obj, sort_keys=True, indent=4)

first_story = hn[0]


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 -

Hardly helpful.

If I print out the type() of the above, it would say it’s a string. I use loads() to convert the above into a list.

I print that out -

Not really looking so good. But it’s a list containing dictionaries. I can extract or modify values as I want.

Now, I can use dumps() to sort of pretty it up when I print it. And that would look like -

There you go. Looks much better. I could use dump and save the above to the file with the same formatting.

A high-level overview, but, hopefully, this starts to clear it up.

Clarified in the content -

we’re going to create a function which will print a JSON object with formatting to make it easier to read.
The function will use the json.dumps() function

1 Like

thank you for the great explanation. this did help clarify loads and dumps. Can surely build on this by reading more