'Valid JSON giving JSONDecodeError: Expecting , delimiter from variable not raw string

Let's imaging the response from the server

{
    "overriding_parameters": {
        "jar_params": [
            "{\"aggregationType\":\"Type1\",\"startDate\":\"2022-05-10\",\"endDate\":\"2022-05-10\"}"
        ]
    }
}

Json is valid: enter image description here

In order to parse it via json.loads(), you need to add r first.

So the result should be

import json
dummy_response = r'{"overriding_parameters": {"jar_params": ["{\"aggregationType\":\"Type1\",\"startDate\":\"2022-05-10\",\"endDate\":\"2022-05-10\"}"]}}'
dummy_dict = json.loads(dummy_response)

But, this server response is coming from airflow xcom:

response = kwargs['ti'].xcom_pull(task_ids='get_run_list')
type(response) # str
metrics = json.loads(response)

How to correctly substitute r in this case? type(response) is str.

I tried this one

json.loads(r"{}".format(response))

But it doesn't work.

How to handle it properly if the response is a variable, not raw str?



Solution 1:[1]

considering your response is what you've provided; couldn't you use json.dumps?

import json

response = {
    "overriding_parameters": {
        "jar_params": [
            "{\"aggregationType\":\"Type1\",\"startDate\":\"2022-05-10\",\"endDate\":\"2022-05-10\"}"
        ]
    }
}

jsonObject = json.loads(json.dumps(response))

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 Shawn Ramirez