<jupyter_start><jupyter_text>Serialization with JSONTo send data over a channel, we need to serialize it, that means, we need to create a sequence of bytes out of it. For many protocols and libraries, it is sufficient to create a string out of the data, the API of the protocol then takes care of the transmission.There are many ways to serialize data into a string. Two of the most commonly used in engineering are:* **XML**: A language for structured data that looks a lot like html, because elements are written as `...`* **JSON**: Another language, originally inspired by JavaScript, that resembles very much Python dicts. **This is what we are going to look at closer in the following.**Let's first import the json library:<jupyter_code>import json<jupyter_output><empty_output><jupyter_text>Serializing DataLet's define some data in the form of Python dicts:<jupyter_code>weight = {'value': 1650, 'unit':'kg'}

register_car = {'id':'register_car', 
           'card_id':'1234', 
            'weight': weight, 
            'passed': True, 
    'recycling_type': None}<jupyter_output><empty_output><jupyter_text>The data above is from a system of recycling stations where a care is weighted, a payment dard registered. The customer can optionally register the type of waste to recycle. Note that we have defined the weight here as its own dictionary, just to show how composite dictionaries can work. The json library can take a dict and generate a string from it:<jupyter_code>payload = json.dumps(register_car)
print(payload)<jupyter_output>{"id": "register_car", "card_id": "1234", "weight": {"value": 1650, "unit": "kg"}, "passed": true, "recycling_type": null}<jupyter_text>That *almost* looks like the original Python dict. This, however, is now a json document that can be read easily by other systems that rone something other than Python. (Json libraries are available for all programming languages worth using.) Note the following differences:- the weight element is expanded and completely contained in the string- The `True` value is now spelled `true`.- The `None` value is now spelled `null`.The payload string is the string that you can send, for instance via HTTP or MQTT.  Deserializing DataAt the receiver side, the library to handle communication will provide you with the bytes or the string of the payload. You just have to unwrap it, using the `json.loads()` function:<jupyter_code>message_payload_receiver = json.loads(payload)
message_payload_receiver<jupyter_output><empty_output><jupyter_text>After the deserialization, the data is a dict again, so that you can easily acess the different values of it.<jupyter_code>print(type(message_payload_receiver))
message_payload_receiver['id']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07798833819241982, 'fraction_numerical': 0.0058309037900874635, 'mean_word_length': 5.398601398601398, 'pattern_counts': {'":': 7, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '53542', 'license': 'MIT', 'n_tokens_mistral': 755, 'n_tokens_neox': 719, 'n_words': 354, 'path': 'Serialization with JSON.ipynb', 'repo_name': 'Kimia97/state-machines-team4', 'size': '2744'}