Search code examples
pythonjsonpandasjsonp

Python Pandas Not Recognizing JSON File


I'm trying to load JSON data in Pandas in order to do some analysis.

Here is an example of the data I'm analyzing.

http://data.ncaa.com/jsonp/game/football/fbs/2013/08/31/wyoming-nebraska/field.json

I have tried the following:

import json
import pandas as pd
from pandas import DataFrame

json_data = pd.read_json('jsonv3.json')

and also

import json
import pandas
from pprint import pprint

json_data=open('jsonv3.json')

data = json.load(json_data)
pprint(data)

json_data.close()

The resulting errors are as follows:

1) ValueError: Expected object or value

2) ValueError: No JSON object could be decoded

I don't really know why the JSON file is not being recognized.

I've confirmed on http://jsonformatter.curiousconcept.com/ That it is valid JSON. I don't really know how to debug the issue. I haven't been able to find anything. Is the error potentially because of the JSON spacing format?


Solution

  • That's not JSON, it is JSONP. Note that the JSON "content" is wrapped in a "function call" callbackWrapper(...). From the wikipedia article: "The response to a JSONP request is not JSON and is not parsed as JSON".

    If you've saved the JSONP response in the file jsonv3.json, you could strip off the function call wrapper and process the content with something like this:

    import json
    
    with open('jsonv3.json', 'r') as f:
        response = f.read()
    
    start = response.find('(')
    end = response.rfind(')')
    json_content = response[start+1:end]
    data = json.loads(json_content)