'How to read JSON that has values in parenthesis with data type?

I have JSON file built like the following:

"key" : DataType("value")

Example -

[
{
"timestamp" : ISODate("2022-03-10T13:50:51.688Z"),
"some_field" : ObjectId("value"),
"normal_key" : "normal_value"
},
{
"different_field" : "just_value"
"key" : "value"
}
]

I can't seem to find a way to read it since as soon as I encounter the datatype I get an error:

JSONDecodeError: Expecting value: line x column y (char z)

which is the location of the first dtype.

Any structured way I could handle this?



Solution 1:[1]

While in my question I was wondering if there is a JSON like way I'm not aware of to handle data types specifications inside the file itself, I realized that string handling might need to be done.

So eventually all I did was to replace the unnecessary substrings with empty strings and later handled the data types in pandas dataframe itself.

with open(path) as f:
    json_data = f.read()

json_data = json_data.replace( 'ISODate(','')
json_data = json_data.replace( 'ObjectId(','')
json_data = json_data.replace( ')','')
print(json_data)

df = pd.read_json(json_data, orient ='records')

Solution 2:[2]

I think if you have custom data like this you can simple create temporary class as a workaround like this:

class TmpClass:
    def __call__(self, val):
        return str(val)
ISODate = TmpClass()
ObjectId = TmpClass()
    

a = [{
"timestamp" : ISODate("2022-03-10T13:50:51.688Z"),
"some_field" : ObjectId("value"),
"normal_key" : "normal_value"
},
{
"different_field" : "just_value",
"key" : "value"
}
]
print(a)

Also if you want custom logic for data types then you can separately implement the two classes with their __call__ method and that will change your data accordingly. I have just changed them to string.

Solution 3:[3]

You can use python's json library.

Let s1 be an object.

jsonstr1 = json.dumps(s1.__dict__)  # <-- serialize the class to a JSON string.

Put the JSON string into the dictionary in place of the object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HermanK
Solution 2 Deepak Tripathi
Solution 3 martineau