I am using a python rest api and want to fetch data from an api which gives me json response with a padding(jsonp request). I have seen few examples where javascript(jquery and angularjs) need to add a callback function for getting json. How to do the same with python?
1 Answer
You don't need to, the padding is only needed in browser/JavaScript context to bypass certain security constraints. For Python usage just take the raw response, remove the padding and parse the remaining data accordingly.
from json import loads
a = 'paddingFunction({"a":1,"b":2,"c":3,"d":4,"e":5})'
startidx = a.find('(')
endidx = a.rfind(')')
print loads(a[startidx + 1:endidx])
>>> {u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}