0

I am calling 3rd party API which is sending response in text/html format. It is a key value pair but not exactly json.

current : { data:"true" ,results:15} expected : "data":"true" , "results":15}

I am trying to parse using json.loads to create a dictionary but as it's clear from the structure it's not exactly json and python throws the error as well.

{JSONDecodeError}Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

Is there a way to fix the response and convert it into proper json or is there any other way where I can get key value pair out of the response

yaml is unable to help me in this case coz, entire data:

{ success:true ,results:9,rows:[{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.5/- Per Share",exDt:"02-Sep-2010",recordDt:"-",bcStartDt:"04-Sep-2010",bcEndDt:"13-Sep-2010",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.8.50 Per Share",exDt:"23-Aug-2011",recordDt:"-",bcStartDt:"25-Aug-2011",bcEndDt:"03-Sep-2011",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.9/- Per Share",exDt:"20-Jul-2012",recordDt:"-",bcStartDt:"24-Jul-2012",bcEndDt:"02-Aug-2012",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting/Dividend Rs 9/- Per Share",exDt:"09-Jul-2013",recordDt:"-",bcStartDt:"11-Jul-2013",bcEndDt:"20-Jul-2013",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2/- Per Share",exDt:"04-Jul-2014",recordDt:"-",bcStartDt:"08-Jul-2014",bcEndDt:"17-Jul-2014",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share",exDt:"28-May-2015",recordDt:"-",bcStartDt:"01-Jun-2015",bcEndDt:"05-Jun-2015",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share",exDt:"29-Jul-2016",recordDt:"-",bcStartDt:"02-Aug-2016",bcEndDt:"11-Aug-2016",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend - Re 0.90 Per Share",exDt:"03-Jul-2017",recordDt:"-",bcStartDt:"05-Jul-2017",bcEndDt:"14-Jul-2017",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend- Re 1 Per Share",exDt:"02-Aug-2018",recordDt:"-",bcStartDt:"04-Aug-2018",bcEndDt:"10-Aug-2018",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"}]}

after using yaml it turns into:

{'success:true': None, 'results:9': None, 'rows': [{'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.5/- Per Share"': None, 'exDt:"02-Sep-2010"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Sep-2010"': None, 'bcEndDt:"13-Sep-2010"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.8.50 Per Share"': None, 'exDt:"23-Aug-2011"': None, 'recordDt:"-"': None, 'bcStartDt:"25-Aug-2011"': None, 'bcEndDt:"03-Sep-2011"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.9/- Per Share"': None, 'exDt:"20-Jul-2012"': None, 'recordDt:"-"': None, 'bcStartDt:"24-Jul-2012"': None, 'bcEndDt:"02-Aug-2012"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting/Dividend Rs 9/- Per Share"': None, 'exDt:"09-Jul-2013"': None, 'recordDt:"-"': None, 'bcStartDt:"11-Jul-2013"': None, 'bcEndDt:"20-Jul-2013"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2/- Per Share"': None, 'exDt:"04-Jul-2014"': None, 'recordDt:"-"': None, 'bcStartDt:"08-Jul-2014"': None, 'bcEndDt:"17-Jul-2014"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share"': None, 'exDt:"28-May-2015"': None, 'recordDt:"-"': None, 'bcStartDt:"01-Jun-2015"': None, 'bcEndDt:"05-Jun-2015"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share"': None, 'exDt:"29-Jul-2016"': None, 'recordDt:"-"': None, 'bcStartDt:"02-Aug-2016"': None, 'bcEndDt:"11-Aug-2016"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend - Re 0.90 Per Share"': None, 'exDt:"03-Jul-2017"': None, 'recordDt:"-"': None, 'bcStartDt:"05-Jul-2017"': None, 'bcEndDt:"14-Jul-2017"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend- Re 1 Per Share"': None, 'exDt:"02-Aug-2018"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Aug-2018"': None, 'bcEndDt:"10-Aug-2018"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}]}

which i can't access like:

yaml.load(corporate_action)['rows'][0]['sym']
2
  • Possible duplicate of How to parse somewhat wrong JSON with Python? Commented May 10, 2019 at 9:00
  • @fluxens have added the entire json like structure which I was unable to parse using yaml. so it was not the solution for me Commented May 11, 2019 at 11:57

1 Answer 1

2

Regex might help here. You can use re.sub

Ex:

import re
import json


data = '{ data:"true" ,results:15}'
data = json.loads(re.sub(r"(\w+):", r'"\1":', data))
print(data)

Output:

{u'data': u'true', u'results': 15}
Sign up to request clarification or add additional context in comments.

1 Comment

I have slightly optimised your solution to suit me. re.sub(r"([a-zA-Z]+):", r'"\1":', data) (\w+) was changing colon occurring in value side as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.