skip to Main Content

I am using openai, Requestesting json response, few times it works fine and few times it failed to load.

Is there any way I can convert unsaturated data to dict and below is example sting

{
    'finishReason': 'STOP',
    'response': '            {                "short_description": "some issue",                "detail_description": "",                "action_items": [                    {                        "action_name": "Raise ticket",                        "assignee": "NOC"                    },                    {                        "action_name": "Involve Java team oncall",                        "assignee": "NOC"                    },                    {                        "action_name": "Involve PKI team",                        "assignee": "NOC"                    }                ],                "followups": [                    "Check if issue is limited to xyz",                    "Check if issue M",                    "Check if issue is related to any latest changes in the configs"                ],                "issue_start_time": ISO 8601 string,                "urgency": 1,                "impact": 1,                "priority": 1            }'
}

I am trying below code

 json.loads(data["response"])

Error:

  Expecting value: line 1 column 1598 (char 1597)

2

Answers


  1. If you manually format the data part, you’ll see that there is unqouted string under "issue_start_time":

    {
        "short_description": "some issue",
        "detail_description": "",
        "action_items": [
            {
                "action_name": "Raise ticket",
                "assignee": "NOC"
            },
            {
                "action_name": "Involve Java team oncall",
                "assignee": "NOC"
            },
            {
                "action_name": "Involve PKI team",
                "assignee": "NOC"
            }
        ],
        "followups": [
            "Check if issue is limited to xyz",
            "Check if issue M",
            "Check if issue is related to any latest changes in the configs"
        ],
        "issue_start_time": ISO 8601 string,
        "urgency": 1,
        "impact": 1,
        "priority": 1
    }
    
    Login or Signup to reply.
  2. yaml parses the data string (already a dict) to a better formatted dict

    import yaml
    
    data = {
        'finishReason': 'STOP',
        'response': '            {                "short_description": "some issue",                "detail_description": "",                "action_items": [                    {                        "action_name": "Raise ticket",                        "assignee": "NOC"                    },                    {                        "action_name": "Involve Java team oncall",                        "assignee": "NOC"                    },                    {                        "action_name": "Involve PKI team",                        "assignee": "NOC"                    }                ],                "followups": [                    "Check if issue is limited to xyz",                    "Check if issue M",                    "Check if issue is related to any latest changes in the configs"                ],                "issue_start_time": ISO 8601 string,                "urgency": 1,                "impact": 1,                "priority": 1            }'
    }
    
    # Use yaml safe loader and convert response to dict
    yaml.load(data["response"], Loader=yaml.SafeLoader)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search