1
00:00:05,120 --> 00:00:09,360
To demonstrate the problem with something that 
JSON doesn't recognise, I'll change our list to 

2
00:00:09,360 --> 00:00:15,280
be a list of tuples, instead of a list of lists.
IntelliJ has a search and replace feature, 

3
00:00:15,280 --> 00:00:19,040
and you can restrict its scope by 
selecting the text that you want to change. 

4
00:00:19,040 --> 00:00:27,268
I'll select all our inner lists, on lines 4 to 11.
From the Edit menu, choose Find > Replace. 

5
00:00:27,360 --> 00:00:32,479
In the top box, which is the text to 
find, enter an opening square bracket 

6
00:00:32,479 --> 00:00:36,673
and in the bottom box, we'll replace 
it with an opening parenthesis. 

7
00:00:36,800 --> 00:00:40,240
Then click Replace All.
We get a lot of errors, 

8
00:00:40,240 --> 00:00:43,280
but they'll go when we 
replace the closing brackets. 

9
00:00:43,280 --> 00:00:47,680
Select the same lines again, and enter a 
closing square bracket for the text to find, 

10
00:00:47,680 --> 00:00:52,720
and replace it with a closing 
parenthesis. Then click Replace All again. 

11
00:00:52,720 --> 00:00:57,200
That's changed the lists to tuples. We 
can close the search and replace bar, 

12
00:00:57,200 --> 00:01:02,960
with the X over on the right.
Ok, uncomment lines 14 and 15, 

13
00:01:02,960 --> 00:01:07,760
and let's see what happens this time:
We're going to write to the test.json file again, 

14
00:01:07,760 --> 00:01:12,570
this time we'll be serializing a list of tuples.
Run the program. 

15
00:01:17,920 --> 00:01:23,360
And we get back a list of lists.
The JSON format doesn't support tuples. 

16
00:01:23,360 --> 00:01:27,360
That's not surprising; many languages 
don't even have tuples, so they'd be 

17
00:01:27,360 --> 00:01:33,710
unable to parse them out of the JSON text.
Remember that JSON is a format for exchanging data. 

18
00:01:33,710 --> 00:01:37,498
As long as you can get the data in a 
form that you can work with, in your code, 

19
00:01:37,498 --> 00:01:41,520
then it's serving its purpose.
JSON has to be language independent, 

20
00:01:41,520 --> 00:01:45,360
otherwise it wouldn't be an open standard 
that can be used with any language. 

21
00:01:45,360 --> 00:01:48,960
That means it can only support objects 
that are common in all languages. 

22
00:01:48,960 --> 00:01:54,000
Let's see what types of data JSON supports. 
There's a table showing the JSON data types 

23
00:01:54,000 --> 00:01:59,003
in the documentation for the json module. 
I'll go to that page in my browser. 

24
00:02:02,480 --> 00:02:06,220
On the left are the eight different 
types of data that you can get from JSON. 

25
00:02:06,220 --> 00:02:09,840
The right hand column shows the 
equivalent Python data types. 

26
00:02:09,840 --> 00:02:14,080
JSON only defines seven data 
types, as shown in that table. 

27
00:02:14,080 --> 00:02:18,800
The Python decoder is clever enough to detect if 
a number is an int or a float, and can return the 

28
00:02:18,800 --> 00:02:25,360
appropriate Python type. But as far as JSON is 
concerned, there's only a single type, number. 

29
00:02:25,360 --> 00:02:31,293
That table shows the JSON types, and the 
corresponding Python type, when a JSON string is decoded – 

30
00:02:31,293 --> 00:02:35,840
such as when we read it from a file.
Scroll down to the next table. 

31
00:02:37,200 --> 00:02:42,880
It's almost the same. This time, it shows the 
Python types on the left, and the corresponding 

32
00:02:42,880 --> 00:02:48,800
JSON type that will be produced, when 
encoding (or serializing) the objects to JSON. 

33
00:02:48,800 --> 00:02:54,161
As you can see, both ints and floats result 
in the same JSON type, which is number. 

34
00:02:54,240 --> 00:02:57,770
We haven't talked about enums 
yet, but they're just numbers. 

35
00:02:57,840 --> 00:03:01,976
So, going back to our code, and 
checking the output in the Run pane, 

36
00:03:02,080 --> 00:03:06,080
the Python decoder has produced lists, 
rather than tuples, because there's 

37
00:03:06,080 --> 00:03:12,678
no specific tuple representation in JSON.
Ok, I'll summarise all this with some slides. 

38
00:03:13,920 --> 00:03:19,680
Use JSON when you want to store data, or transmit 
it, in a format that other systems can use. 

39
00:03:19,680 --> 00:03:24,880
JSON isn't suitable for storing your program's 
data, if you need to preserve the exact type. 

40
00:03:24,880 --> 00:03:29,600
As we've seen, it can only be used to 
represent a limited number of data types. 

41
00:03:29,600 --> 00:03:33,871
Those types are usually sufficient, when 
you're only interested in the actual data. 

42
00:03:34,000 --> 00:03:37,520
They're not suitable when the exact 
type of the object is important. 

43
00:03:37,520 --> 00:03:41,520
That's rarely the case, when transferring 
JSON, but is important if you want to 

44
00:03:41,520 --> 00:03:46,331
save data in your program, and get 
back exactly what you saved, later. 

45
00:03:48,720 --> 00:03:52,320
The main limitation of JSON is related 
to the problem we've already seen: 

46
00:03:52,320 --> 00:03:56,880
JSON only supports seven data types.
In particular, there's no support 

47
00:03:56,880 --> 00:04:00,160
for things like dates.
The decoding program needs to 

48
00:04:00,160 --> 00:04:04,720
know how you've stored your dates in strings.
There is an international standard for storing 

49
00:04:04,720 --> 00:04:10,960
dates (google ISO 8601 for details). 
When including dates in your JSON data, 

50
00:04:10,960 --> 00:04:15,111
we definitely recommend sticking to 
one of the formats in that standard. 

51
00:04:17,360 --> 00:04:20,531
Related to that is the lack of typing, in JSON. 

52
00:04:20,640 --> 00:04:25,360
The JSON format doesn't provide any 
indication about the type of data it contains. 

53
00:04:25,360 --> 00:04:30,960
Because it has a limited number of data types, a 
JSON parser can get the data back quite easily. 

54
00:04:30,960 --> 00:04:36,000
But it's then up to the program consuming the data 
to work out what each piece of data represents. 

55
00:04:36,000 --> 00:04:41,520
Dates, once again, are a good example of 
this. After parsing the data to get a string, 

56
00:04:41,520 --> 00:04:47,010
the program will need to know the exact format 
that was used, to represent things like dates. 

57
00:04:49,040 --> 00:04:55,635
Another example is representing complex numbers. 
The usual way, in JSON, is to use a list. 

58
00:04:55,635 --> 00:05:02,160
For example 12.5 + 3i would be stored as 
[12.5, 3] and it's left to the decoding 

59
00:05:02,160 --> 00:05:06,000
program to know that a complex number 
is present at that position in the data. 

60
00:05:06,000 --> 00:05:12,240
A complex number can be specified in XML, to give 
one example, with code such as the XML below: 

61
00:05:12,240 --> 00:05:15,680
JSON is far less verbose than 
some other serialization formats, 

62
00:05:15,680 --> 00:05:21,340
such as XML, but as a consequence, it 
lacks the ability to specify the data type. 

63
00:05:23,760 --> 00:05:28,480
Despite those limitations, JSON has 
become a popular format on the internet. 

64
00:05:28,480 --> 00:05:33,392
It's size is often many times 
smaller than formats such as XML, for example. 

65
00:05:33,392 --> 00:05:36,240
That can be important when 
you want a web page to load quickly, 

66
00:05:36,240 --> 00:05:39,828
without having to wait while a 
large amount of data is downloaded. 

67
00:05:39,828 --> 00:05:44,000
JSON is also easy to parse – in 
part because of its simplicity. 

68
00:05:44,000 --> 00:05:48,480
Encoders and decoders are available for 
most programming languages in use today. 

69
00:05:48,480 --> 00:05:52,640
It's also very easy to read and 
edit. Whitespace is unimportant 

70
00:05:52,640 --> 00:05:58,228
(except inside strings of course) and it's quite 
hard to mess up a JSON document by accident. 

71
00:05:59,440 --> 00:06:03,200
In the next couple of videos, we'll 
fetch some JSON data from the internet, 

72
00:06:03,200 --> 00:06:08,033
and use it in our Python code.
I'll see you, in the next video.

