Exercise: Convert User Record To JSON And Back

Requirement

In Exercise: Refactoring - Extract Both CSV Formats Into Module, we created a set of routines around user records of the following form:

{
    'id': 2,    # int
    'firstname': 'Jörg',
    'lastname': 'Faschingbauer',
    'birth': '19.6.1966',
}

Preparing for MQTT communication (Exercise: MQTT: Publish User Records), it is advisable to create a module userdb_json.py that defines a transport format for transmission that is

  • Converted to before sending

  • Converted from after receiving

Usage is envisioned as follows:

import userdb_csv
import userdb_json

for user in userdb_csv.read_csv_noheader('some.csv'):   # could be ANY source, of course
    json_str = userdb_json.to_json(user)
    ... publish json_str to MQTT topic ...

Test Code

The following test (see pytest Introduction, By Example for how to use it) can better express the requirement,

import userdb_json


def test_user_to_json():
    user_sent = {
        'id': 1,
        'firstname': 'Jörg',
        'lastname': 'Faschingbauer',
        'birth': '19.6.1966',
    }

    json_user_sent = userdb_json.to_json(user_sent)
    user_received = userdb_json.from_json(json_user_sent)

    assert user_received['id'] == 1
    assert user_received['firstname'] == 'Jörg'
    assert user_received['lastname'] == 'Faschingbauer'
    assert user_received['birth'] == '19.6.1966'    

Dependencies

cluster_python Python Programming cluster_python_basics Python: The Language Fundamentals cluster_python_exercises Exercises cluster_python_exercises_userdb User Database (Exercise Series) cluster_python_misc Python: Miscellaneous Topics cluster_python_advanced Python: More Language Features python_basics_python_0139_commandline_argv Commandline Arguments (sys.argv) python_basics_python_0125_running Running Python Programs python_basics_python_0139_commandline_argv->python_basics_python_0125_running python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0139_commandline_argv->python_basics_python_0130_syntax_etc python_basics_python_0140_variables Variables python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0120_helloworld Hello World python_basics_python_0110_blahblah Blahblah python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0500_files File I/O python_basics_python_0220_for for Loops python_basics_python_0500_files->python_basics_python_0220_for python_misc_encoding Encoding python_basics_python_0500_files->python_misc_encoding python_basics_python_0270_functions Functions python_basics_python_0270_functions->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0270_functions->python_basics_python_0150_datatypes_overview python_basics_python_0300_strings More About Strings python_basics_python_0250_refs_flat_deep_copy References, (Im)mutability python_basics_python_0300_strings->python_basics_python_0250_refs_flat_deep_copy python_basics_python_0300_strings->python_basics_python_0150_datatypes_overview python_basics_python_0200_sequential_types Sequential Datatypes python_basics_python_0300_strings->python_basics_python_0200_sequential_types python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0140_variables python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview_compound python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0125_running->python_basics_python_0120_helloworld python_basics_python_0200_sequential_types->python_basics_python_0150_datatypes_overview_compound python_basics_python_0160_boolean Boolean python_basics_python_0160_boolean->python_basics_python_0150_datatypes_overview python_basics_python_0193_while while Loops python_basics_python_0193_while->python_basics_python_0160_boolean python_basics_python_0170_if The if Statement python_basics_python_0193_while->python_basics_python_0170_if python_basics_python_0170_if->python_basics_python_0160_boolean python_basics_python_0320_strings_methods Miscellaneous String Methods python_basics_python_0320_strings_methods->python_basics_python_0300_strings python_basics_python_0220_for->python_basics_python_0200_sequential_types python_basics_python_0220_for->python_basics_python_0193_while python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_exercises_userdb_csvreader Exercise: Read CSV File (csv.reader) python_exercises_userdb_csvreader->python_basics_python_0139_commandline_argv python_misc_csv CSV Files python_exercises_userdb_csvreader->python_misc_csv python_exercises_userdb_userdb_module Exercise: Refactoring - Extract Both CSV Formats Into Module python_exercises_userdb_userdb_module->python_exercises_userdb_csvreader python_exercises_userdb_csvdictreader_function Exercise: Refactoring - Extract CSV Reading Into Function (csv.dictreader) python_exercises_userdb_userdb_module->python_exercises_userdb_csvdictreader_function python_advanced_modules Modules and Packages python_exercises_userdb_userdb_module->python_advanced_modules python_exercises_userdb_csvdictreader_function->python_basics_python_0270_functions python_exercises_userdb_csvdictreader Exercise: Read CSV File (csv.dictreader) python_exercises_userdb_csvdictreader_function->python_exercises_userdb_csvdictreader python_exercises_userdb_user_json Exercise: Convert User Record To JSON And Back python_exercises_userdb_user_json->python_exercises_userdb_userdb_module python_exercises_userdb_csvdictreader->python_basics_python_0139_commandline_argv python_exercises_userdb_csvdictreader->python_exercises_userdb_csvreader python_exercises_userdb_csvdictreader->python_misc_csv python_misc_csv->python_basics_python_0500_files python_misc_csv->python_basics_python_0150_datatypes_overview_compound python_misc_csv->python_basics_python_0220_for python_misc_encoding->python_basics_python_0150_datatypes_overview python_misc_encoding->python_basics_python_0320_strings_methods