Home > Mobile >  How to read JSON that has values in parenthesis with data type?
How to read JSON that has values in parenthesis with data type?

Time:04-30

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related