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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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')

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x