Deserialize a JSON string into a Python list

The string s in line 11 is a serialization of a Python list of numbers. A serialization of a list is a representation of the list as a string of characters (or as a sequence of bytes). Since we have serialized the list in JSON format, the characters are digits, spaces, commas, and square brackets. In fact, it looks just like the way you would write the list in the language Python.

The json.loads function in line 18 deserializes the list of numbers, i.e., converts the list of numbers from a string of characters (that merely looks like a list of numbers) into an actual list of numbers. The name loads stands for “load string”.

The json.dumps function in line 34 serializes the list of numbers, i.e., converts the list of numbers back into a string of characters. The name dumps stands for “dump string”.

A text file on the disk can hold a series of characters. We serialize a list of numbers so that the list can be stored in a text file. We deserialize the characters read from a text file so that we can recreate the list of numbers.

jsonlist.py

type(s) = <class 'str'>
s = [10, 20, 30, 40,50]

type(listOfInts) = <class 'list'>
10
20
30
40
50

Pretty print the listOfInts.
[10, 20, 30, 40, 50]

Things to try

  1. If we inserted
    for c in s:
        print(c)
    
    at line 15, the output would be
    [
    1
    0
    ,
    
    2
    etc.
    

  2. Change line 34 from
        print(json.dumps(listOfInts))
    
    Pretty print the listOfInts.
    [10, 20, 30, 40, 50]
    
    to
        print(json.dumps(listOfInts, indent = 4))
    
    Pretty print the listOfInts.
    [
        10,
        20,
        30,
        40,
        50
    ]
    

  3. Keeping the indent in line 34, change the string s to the following, taken from Heterogeneous but with the underscores removed from the numbers. The fact that the underscores are illegal in a JSON number shows that JSON is not exactly the same as Python.
    s = """\
    [
        ["Dr. No",                1962,  59500000.00, ["Sean Connery", "Ursula Andress", "Joseph Wiseman", "Jack Lord"]],
        ["From Russia With Love", 1963,  79000000.00, ["Sean Connery", "Pedro Armendáriz", "Lotte Lenya", "Robert Shaw", "Bernard Lee", "Daniela Bianchi"]],
        ["Goldfinger",            1964, 125000000.00, ["Sean Connery", "Honor Blackman", "Gert Fröbe"]],
        ["Thunderball",           1965, 141200000.00, ["Sean Connery", "Claudine Auger", "Adolfo Celi", "Luciana Paluzzi", "Rik Van Nutter"]],
        ["Casino Royale",         1967,  41700000.00, ["Peter Sellers", "Ursula Andress", "David Niven", "Woody Allen", "Joanna Pettet", "Orson Welles", "Daliah Lavi"]]
    ]"""
    
    And change listOfInts to listOfMovies.
    type(s) = <class 'str'>
    s = [
        ["Dr. No",                1962,  59500000.00, ["Sean Connery", "Ursula Andress", "Joseph Wiseman", "Jack Lord"]],
        ["From Russia With Love", 1963,  79000000.00, ["Sean Connery", "Pedro Armendáriz", "Lotte Lenya", "Robert Shaw", "Bernard Lee", "Daniela Bianchi"]],
        ["Goldfinger",            1964, 125000000.00, ["Sean Connery", "Honor Blackman", "Gert Fröbe"]],
        ["Thunderball",           1965, 141200000.00, ["Sean Connery", "Claudine Auger", "Adolfo Celi", "Luciana Paluzzi", "Rik Van Nutter"]],
        ["Casino Royale",         1967,  41700000.00, ["Peter Sellers", "Ursula Andress", "David Niven", "Woody Allen", "Joanna Pettet", "Orson Welles", "Daliah Lavi"]]
    ]
    
    type(listOfMovies) = <class 'list'>
    ['Dr. No', 1962, 59500000.0, ['Sean Connery', 'Ursula Andress', 'Joseph Wiseman', 'Jack Lord']]
    ['From Russia With Love', 1963, 79000000.0, ['Sean Connery', 'Pedro Armendáriz', 'Lotte Lenya', 'Robert Shaw', 'Bernard Lee', 'Daniela Bianchi']]
    ['Goldfinger', 1964, 125000000.0, ['Sean Connery', 'Honor Blackman', 'Gert Fröbe']]
    ['Thunderball', 1965, 141200000.0, ['Sean Connery', 'Claudine Auger', 'Adolfo Celi', 'Luciana Paluzzi', 'Rik Van Nutter']]
    ['Casino Royale', 1967, 41700000.0, ['Peter Sellers', 'Ursula Andress', 'David Niven', 'Woody Allen', 'Joanna Pettet', 'Orson Welles', 'Daliah Lavi']]
    
    Pretty print the listOfMovies.
    [
        [
            "Dr. No",
            1962,
            59500000.0,
            [
                "Sean Connery",
                "Ursula Andress",
                "Joseph Wiseman",
                "Jack Lord"
            ]
        ],
        [
            "From Russia With Love",
            1963,
            79000000.0,
            [
                "Sean Connery",
                "Pedro Armend\u00e1riz",
                "Lotte Lenya",
                "Robert Shaw",
                "Bernard Lee",
                "Daniela Bianchi"
            ]
        ],
        [
            "Goldfinger",
            1964,
            125000000.0,
            [
                "Sean Connery",
                "Honor Blackman",
                "Gert Fr\u00f6be"
            ]
        ],
        [
            "Thunderball",
            1965,
            141200000.0,
            [
                "Sean Connery",
                "Claudine Auger",
                "Adolfo Celi",
                "Luciana Paluzzi",
                "Rik Van Nutter"
            ]
        ],
        [
            "Casino Royale",
            1967,
            41700000.0,
            [
                "Peter Sellers",
                "Ursula Andress",
                "David Niven",
                "Woody Allen",
                "Joanna Pettet",
                "Orson Welles",
                "Daliah Lavi"
            ]
        ]
    ]