SPARQL query response to objects

35 Views Asked by At

I have a SPARKQL query that send me a response in json.

    "results": {
        "response": [
            {
                "nameinfo": {
                    "data": "data1"
                },
                "namedetails": {
                    "data": "data2"
                },
                "childinfo": {
                    "data": "data3"
                },
                "childdetails": {
                    "data": "data4"
                },
                "parentinfo": {
                    "data": "data5"
                },
                "parentdetails": {
                    "data": "data6"
                },
                "prop": {
                    "data": "data7"
                }
            },
           {
             ......
           }
]
}
for data in response["results"]["response"]:
            
            data_dict = {
                "nameinfo": "",
                "namedetails": "",
                "childinfo": "",
                "childdetails": "",
                "parentinfo": "",
                "parentdetails": "",
                "prop": "",
            }
            if "nameinfo" in data:
                data_dict["nameinfo"] = data[
                    "nameinfo"]["data"]
            if "namedetails" in data:
                data_dict["namedetails"] = data[
                    "namedetails"]["data"]
            if "childinfo" in data:
                data_dict["childinfo"] = data[
                    "childinfo"
                ]["value"]
            ...
            hierarchy.append(data_dict)

        hierarchy = build_heirarchy(
            hierarchy, parent=data_dict["nameinfo"]
        )
def build_heirarchy(data, parent=None):
//recursive funtion to build the heirarchy

I need to iterate the response to create an object in Python. I am using for in and if else and recursive aproach. But it is very timeconsuming and the performance is slow. I am iterating the json object and adding to the Pydantic Model I have used.

Is there way to make it perform better, faster ?

0

There are 0 best solutions below