I am trying to convert a JavaScript object into a Python dict for processing and later export to JSON. So far easy peasy by using demjson3.

BUT: since there are undefined values it will convert these into demjson3.undefined.
Those demjson3.undefined are not JSON serializable and therefore raise a TypeError.

I am aware of what the difference of null and undefined is, but since there is no Python equivalent for undefined what is best practice to handle it? IMHO: None

edit: the served JS obj is not under my control and I have (in this use case) to process what is being served.

Is there a demjson3 parameter I am not yet aware of, do i actually have to provide a custom serializer for something so obvious or is some_js_obj.replace('undefined', 'null') the way to go?

How do you handle this?

Example:

some_js_obj = locationAddress: {
      geoCode: "133713371337",
      city: "Berlin",
      zip: "10318",
      isFullAddress: false,
      houseNumber: undefined,
      street: undefined,
      qualifiedGeoIds: {
        continentId: "1",
        countryId: "111",
        regionId: "3",
        cityId: "42",
        districtId: "13"
      }
    }

converts into using demjson3:

some_py_dict = {'locationAddress': {'geoCode': '133713371337', 
'city': 'Berlin', 
'zip': '10318', 
'isFullAddress': False, 
'houseNumber': demjson3.undefined, 
'street': demjson3.undefined, 
'qualifiedGeoIds': {
'continentId': '1', 
'countryId': '111', 
'regionId': '3', 
'cityId': '42', 
'districtId': '13'}}}

6 Replies 6

You are trying to convert js-undefined to python-something to json-something.

json has no undefined and neither does python (as you say), it is a very js-specific concept.

I think you should start by thinking about why you have undefined in your js-objects, because normally, you should not. If you can not get rid of it, think about why, narrow down what purpose they serve, what they mean in your domain. Then, express that meaning in some other way when converting to python.

Python's None is equivalent to both javascript's null and undefined. Because, you are right, there is not a proper equivalent to undefinedin Python.

I use None without hesitation. At the end of the day, they null and undefined make no difference for the consumer of the json.

I also think that the decision of demjson3 creating an adhoc object demjson3.undefined to represent the original undefined is unnecessary and even prevents further applicability like the case of generating json properly.

The js-undefined is not under my control.
Therefore the question is on How to handle it best.

Why at all do you use demjson3 ? Python and javascript have builtin JSON support.

I am aware of that, but this is also not under my control and I have to process what is being served 🤷‍♂️

I would try monkey patching the definition of demjson3.undefined. This seems to work for the simplest test.

print(undefined)
print(decode("""{foo: undefined}"""))

undefined = None
print(undefined)
print(decode("""{foo: undefined}"""))

giving me:

undefined
{'foo': __main__.undefined}
None
{'foo': None}

Note that I just copied the source of this module to a test file as I did not actually install it so I imagine if you are importing it, you might try:

demjson3.undefined = None

Your Reply

By clicking “Post Your Reply”, 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.