Welcome back. Nested lists and
dictionaries are very commonly shared between computer systems.
A standard format has been defined that makes it easy to share that data.
It's called JSON, J-S-O-N which stands for JavaScript Object Notation.
It originated with a different programming language JavaScript.
But, Python makes it pretty easy to both read in and write out data in JSON format.
In fact, JSON format looks almost
exactly like the printout of a Python list or a dictionary.
In most cases, you wouldn't really notice the difference,
but there are a couple of small differences;
like the Python value none being represented as the word null,
and true and false not being capitalized.
We'll use the JSON module to read in and write out data in JSON format.
Little warning, the JSON module that we have in Runestone
only does a subset of what the full JSON module and a full Python environment would do.
But we have the two most useful functions.
Let's take a look at them.
The first one is called Load S. It takes a string as input,
and it returns a dictionary or a list.
So, here we have little code snippet.
We're importing the JSON module,
and then we've got a variable called A string whose value is naturally a string,
and that string has some newline characters in it,
and then it's got the curly brace which
would suggest to you that it's going to be a Python dictionary.
It's really just a string,
but once we take that string and on line three,
we pass it as an input to the Load S function,
which is part of the JSON module.
The Load S function,
in the JSON module,
takes a string as an input and as an output it produces
either a Python list or
a Python dictionary depending on what the contents of the string were.
So, in this case the string has its first non blank space its non white space character,
the first one is a curly brace.
So, what's going to be output is a dictionary.
So on line four,
we print out the type of D,
and it is a dictionary.
Since it's a dictionary,
we can ask for its keys and it has two keys;
result count and results.
We can ask for the value associated with result count and it's 25.
That's the 25 that you see there in the string.
One important thing to keep track of here,
is when you have a string,
and when you have a dictionary.
So, we start with something that's a character string,
and by passing it in to the Load S function,
we get a dictionary as an output.
If I lost track of that let's say,
and I tried to print a_string square bracket result count.
Because a_string kind of looks like a dictionary,
and I try to run this, I'm going to get an error.
It says its string indices must be integers not strings on line seven.
Let's say on line seven,
we're trying to treat a_string as if it's a dictionary.
It isn't a dictionary, it's a string.
We're allowed to ask for a_string square bracket four to get the fourth character,
but we can't ask for a_string square bracket result count.
We can ask for D square bracket result count,
because D is a dictionary.
The second useful function from the JSON module is called Dump S.
It does the opposite of Load S. It takes a Python list or dictionary,
even a nested one,
and converts it into a string that's in the JSON format.
Once we have a string in the JSON format,
we can write it to a file or do anything else that we would do with a string.
The Dump S function always takes a list or a dictionary as an input.
But it also can take a couple of other parameters.
If you pass sort keys equals true, then,
whenever you have a dictionary it's going to output those in alphabetic order.
If you pass the indent parameter,
it will pretty print the string.
It'll do some indentation in line breaks.
You can specify how many characters of indentation you want.
So let's see what happens when we run this.
Oops! I got an error.
It's telling me that on line two,
JSON is not defined.
The reason is that I forgot,
and I need to import the JSON module.
So, the Dump S function is in the JSON module,
and this code snippet didn't have the JSON module imported.
So let me run it again.
Now you can see, when I pass D to the pretty function on line nine,
that it is creating a string by calling the json.dumps function and that string,
because I said indent equals two,
each time I descend one level into this next to the data structure,
it's adding two more levels, two more spaces.
That makes it just a lot easier for a person to read when I print out that string.
Now, you may have noticed little funny pronunciation that I've done here.
Instead of calling this Dumps and Loads,
I've called it Dump S and Load S. That's very deliberate.
It helps me to remind me that that S is for string.
The S is not for plural.
You still have to remember which of Dump S and
Load S reads from a string and which one writes to a string.
Here's how I think of it.
The JSON format is a set of conventions for how a string
should be structured so that it can be loaded into a Python object.
Thus, Load S means load from a string or load a string into an object.
Dump S means dump an object to a string.
Dumping from a string wouldn't really make sense,
so that's how I can keep track of it.
Dump S, dump an object to a string.
Load S, load an object from a string.
That's the JSON format folks.
We'll be seeing a lot more of it when we look at
rest APIs that let us fetch data from servers on the internet,
like from iTunes or OMDb.
JSON dot Load S,
loads from a string,
into a Python object,
either a list or a dictionary,
and JSON dot dump S dumps from a Python list or dictionary into a string.
JSON dot Dump S,
with the indent parameter,
yields a string with indentation,
that makes it easier for people to read and understand. See you next time.