1

I've a dictionary which looks like the example I've mentioned below. I need to save it as a json file in the same format without changing the data types to a string value so that it can be imported later on to validate the data type of the parameters used.

data = {
    'model':{
        'param1': tuple,
        'param2': tuple
    },
    'model2':{
        'param3': int,
        'param4': bool
    }
}

It is being used like this:

isinstance(some_value, data['model']['param_1'])

Here some_value is the value for which we need to check the type.

5
  • it's a nested dictionary tho Commented Apr 29, 2022 at 13:39
  • JSON has int and bool datatypes, but tuple isn't a thing. JSON serialization will turn your tuples into lists. Commented Apr 29, 2022 at 13:39
  • 2
    If your values are not converted to valid JSON types then your output won't be JSON. Does your output have to be JSON? Have you considered pickle? Commented Apr 29, 2022 at 13:48
  • It's impossible to convert values like tuple or bool to JSON, so if you want to use JSON you'll have to convert them to a different representation, for example strings. This still allows you to use the isinstance checks that you want, so long as you convert them back to the original type from the serialized representation. Commented Apr 29, 2022 at 14:10
  • Saving dictionary as a pickle file does this job. Thanks @LancelotduLac Commented Apr 29, 2022 at 15:18

2 Answers 2

1

Some of this was already covered in the comments...

Unfortunately the only data types that can be stored in a json file are those that are described in the JSON specification, which includes arrays, objects, strings, numbers, booleans and null.

When you load a json file in using pythons json module, it automatically converts certain JSON Data Types into the python equivalent. e.g. arrays turn into lists, objects turn into dicts, strings remain strings, null becomes None etc... so what you are asking is kind of impossible.

There are of course ways around this, such as instead of storing tuple which is a type object in python, you can instead store the "tuple" keyword as a string and then write some conversion logic you can run when needed.

Probably a better alternative though is to use the pickle module. The api is identical to the json module, and it can store all python data types with ease, including custom classes in most cases. Pickled files are not human readable unfortunately, but your use case doesn't sound like that is a requirement.

Here is a link to the documentation on the pickle module. https://docs.python.org/3/library/pickle.html

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using a json file, I converted the nested dictionary to yaml file and then later used it to check the data types of the supplied parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.