0

I'm trying to open a JSON file with pandas but I'm receiving multiple errors:

df = pd.read_json('offers_feed_01.json', lines = True, orient = "split")

I run this code and received:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-ba348dcc5991> in <module>
----> 1 df = pd.read_json('offers_feed_01.json', lines = True, orient = "split")

/opt/anaconda3/lib/python3.8/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    197                 else:
    198                     kwargs[new_arg_name] = new_arg_value
--> 199             return func(*args, **kwargs)
    200 
    201         return cast(F, wrapper)

/opt/anaconda3/lib/python3.8/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    294                 )
    295                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 296             return func(*args, **kwargs)
    297 
    298         return wrapper

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/json/_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows)
    616         return json_reader
    617 
--> 618     result = json_reader.read()
    619     if should_close:
    620         filepath_or_buffer.close()

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/json/_json.py in read(self)
    751                 data = ensure_str(self.data)
    752                 data = data.split("\n")
--> 753                 obj = self._get_object_parser(self._combine_lines(data))
    754         else:
    755             obj = self._get_object_parser(self.data)

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/json/_json.py in _get_object_parser(self, json)
    775         obj = None
    776         if typ == "frame":
--> 777             obj = FrameParser(json, **kwargs).parse()
    778 
    779         if typ == "series" or obj is None:

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/json/_json.py in parse(self)
    884 
    885         else:
--> 886             self._parse_no_numpy()
    887 
    888         if self.obj is None:

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
   1122             decoded = {
   1123                 str(k): v
-> 1124                 for k, v in loads(json, precise_float=self.precise_float).items()
   1125             }
   1126             self.check_keys_split(decoded)

ValueError: Expected object or value

The file is in the correct place, I already changed the orient multiple times and I ran another similar file with the same code and it worked. The .json file I'm running is the following:

 {
  "offers": [
    {
      "offerType": "mobile-app",
      "linkDest": "app-store",
      "convertsOn": "install-and-open",
      "appInfo": {
        "appID": "",
        "previewLink": "",
        "appName": "Housejoy",
        "appCategory": "Lifestyle",
        "appIcon": ""
      },
      "targets": [
        {
          "offerID": "",
          "approvalStatus": "approved",
          "offerStatus": "active",
          "trackingLink": "",
          "countries": [
            "IN"
          ],
          "platforms": [
            "iphone"
          ],
          "payout": {
            "amount": 0.94,
            "currency": "USD"
          },
          "endDate": null,
          "dailyConversionCap": null,
          "restrictions": {
            "allowIncent": false,
            "deviceIDRequired": false,
            "sourceAppIDRequired": false,
            "minOSVersion": "9.0",
            "allowedPlacements": null,
            "blockedPlacements": []
          }
        }
      ]
    },

I already tried removing the {"offers": but also didn't work. Anybody knows what could be the problem?

3
  • Could you please edit to include the full traceback? What are the other errors? Commented Dec 23, 2020 at 11:24
  • Please show the traceback along with the error, not just "ValueError: Expected object or value". Commented Dec 23, 2020 at 11:24
  • I edited with the full message. Thanks Commented Dec 23, 2020 at 11:38

2 Answers 2

1

try opening and reading the data, then using pandas to read the data. What you'll find is several nested lists which aren't parsed to a level you may want. But see if this gets you started.

with open('yourjsonfile.json', 'rb') as f:
    data = f.read().decode('utf-8')

df = pd.json_normalize(data['offers']) 
Sign up to request clarification or add additional context in comments.

Comments

0

The .json file is malformed. Verify that with some tool like https://jsonlint.com/

2 Comments

It says that is valid
the content what you copied is not valid

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.