Home > Net >  How to convert nested string dictionary without quotes to dictionary in Python
How to convert nested string dictionary without quotes to dictionary in Python

Time:09-30

I have below nested string dictionary without quotes which I want to convert into python dictionary.

                { id: 0,
                  label: 'Data0',
                  axis: "left",
                  color: "#0000ff",
                  avg: "383.04347826086956",
                  last: "378.0",
                  min: "282.0",
                  max: "439.0" }
                ,
                { id: 1,
                  label: 'Data1',
                  axis: "left",
                  color: "#00ff00",
                  avg: "",
                  last: "",
                  min: "",
                  max: "" }

Expected Output:

                { "id": 0,
                  "label": "Data0",
                  "axis": "left",
                  "color": "#0000ff",
                  "avg": 383.04347826086956,
                  "last": 378.0,
                  "min": 282.0,
                  "max": 439.0 }
                ,
                { "id: 1,
                  "label": "Data1",
                  "axis": "left",
                  "color": "#00ff00",
                  "avg": "",
                  "last": "",
                  "min": "",
                  "max": "" }

The main reason for doing this is am getting output as string from API response where it contains many other things which I had removed by using split() method.

CodePudding user response:

Try:

js_data = """
/*
 * Pure Javascript, which calls the specified callback function, specified using the Jsonp parameter
 *
 * Callback function is passed all parameters necessary to render chart
 */

MP.ChartController.loaded('chartdiv',
{
    error: '',
    width: 1480,
    height: 308,
    summaryData: [

        {
            id: 0,
            label: 'Data0',
            axis: "left",
            color: "#0000ff",
            avg: "383.04347826086956",
            last: "378.0",
            min: "282.0",
            max: "439.0"
        },
        {
            id: 1,
            label: 'PQ Initiated',
            axis: "left",
            color: "#00ff00",
            avg: "",
            last: "",
            min: "",
            max: ""
        }
    ],
    graphType: 'chart',
    warnings: []
});
"""

import re
import json

# find `summaryData`
summary_data = re.search(r"summaryData: (\[.*?\]),", js_data, flags=re.S)

# add quotes("") around keys
summary_data = re.sub(r"(\S ):", r'"\1":', summary_data.group(1))

# replace ' to "
summary_data = summary_data.replace("'", '"')

# decode the string:
summary_data = json.loads(summary_data)

print(summary_data)

Prints:

[
    {
        "id": 0,
        "label": "Data0",
        "axis": "left",
        "color": "#0000ff",
        "avg": "383.04347826086956",
        "last": "378.0",
        "min": "282.0",
        "max": "439.0",
    },
    {
        "id": 1,
        "label": "PQ Initiated",
        "axis": "left",
        "color": "#00ff00",
        "avg": "",
        "last": "",
        "min": "",
        "max": "",
    },
]
  • Related