Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Serialization-modules
JSON Module

Objective

Go to the service station and make sure data is properly stored by using JSON module to format files.

There’s a service station south of the annex building in the new construction area, this entire area is going to be redeveloped to plant crops. The station has a variety of terminals processing data for the new development, we can work writing the data manually, but that would take a lot of time. For the process of handling data we can use the JSON which stands for JavaScript Object Notation, this is a data format that is independent of programming languages and stores information in an easy readable for data storage and transmission.

In order to use JSONs with python we can use import json which will give us access to JSON functions, for our purposes we will be using the following functions:

  • json.loads(): Converts JSON string into a Python data, takes one argument, that being the string you wish to parse into Python.
  • json.dumps(): Converts Python data into JSON string object, this function can take up to four(4) arguments, only the first one is a requirement to use the function. They are as follows.
    1. Python data you wish to convert into JSON: they can be of the following data types: dictionaries, lists, tuples, string, integer, float, boolean, and None data types.
    2. indent: number of spaces you’d like to add at the beginning of the data point
    3. separators: these are the portions you would like to appear between and/or at the end of data points.
    4. sort_keys: sorts dictionary keys alphabetically if you’re converting a dictionary file type.

To start off, head to the gold X mark in the outpost to the left and face the desk with the memo. Use the read() function to verify information required to confirm the status of the terminal and make note of it. Walk to the light X mark in front of the terminal, there’s a constant named transmission , use json.dumps() to format it as a json so we may convert it, like this: transmission = json.dumps(transmission) .

Create a dictionary named load and store the value of json.loads() with the transmission constant as an argument. This will convert the Json into Python so it’s information can be easily accessed. Use the the speak() function with the keys outlined in the memo and using the load dictionary you created. For example: player.speak(load["system"]) , do this for all the keys.

After checking the information on the terminal walk down towards the station, go inside and head to the light X mark over the green carpet. Check the memo using the read() function, check and make note of the values, in the editor there is a pre-written dictionary named profile, fill out the missing values with the information in the memo.

Walk to the dark X mark over the green carpet, face the terminal and create a variable named transfer and store the value of json.dumps() and add the profile dictionary as the only argument. By doing this we convert the dictionary into a json string object. Use the pre-written display() function and add the transfer Json to store the data in the terminal.

Walk to the dark X mark over the red carpet and use read() function to verify the data already entered in the terminal. The data isn’t presented in a proper format, in order to properly store the information it needs to be stored and formatted in a Json. The data is stored in a dictionary constant called red_data, create a variable named red_storage and store the value of the json.dumps() function.

In order to format the data, add the following arguments to the function: add red_data to convert the constant to Json, add indent and set it to 5 to format the text, add separators and add ("- ", " = ") to add these symbols at the end of each line. The line should look like this: red_storage = json.dumps(red_data, indent=5, separators=("- ", " = ")) . Use the pre-written display() and add the red_storage Json string to store and display the formatted data.

Walk to the dark X mark over the blue carpet and face the terminal and use the read() function, much like before the data is not stored properly and needs to be formatted but also sorted. The data is stored in a constant named blue_data , create a variable named blue_storage and store the value of the json.dumps() function.

For arguments: add blue_data to convert the constant to Json, add indent and set it to 4 to format the text, add separators and add (".0", " = ") to add these symbols at the end of each line. In addition to these also add the argument sort_keys as the final argument and set it to True, like this: sort_keys=True . Use the pre-written display() and add the blue_storage Json string to store and display the sorted data and complete the level.

Code book