I am new to machine learning and I am trying to build a classifier. My problem is that I am not able to import the dataset I need. In particular, I put my dataset in the Desktop and what I did is:

import numpy as np
import pandas as pd
import jsonlines                   #edit

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import *
from sklearn.naive_bayes import *
from sklearn.metrics import confusion_matrix, classification_report
from sklearn import svm

#for visualizing data
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(font_scale=1.2)

%matplotlib inline

print('Libraries imported.')

now, after these imports I want to use the function

training_set = pd.read_json('\Desktop\training_dataset.jsonl')  #edit

to import my dataset. The problem is that what i get is:

ValueError                                Traceback (most recent call last)
<ipython-input-16-f789503c3c7c> in <module>
----> 1 training_set = pd.read_json('\Desktop\training_dataset.json')
      2 print(training_set.head())

~\Anaconda3\lib\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)
    590         return json_reader
--> 592     result = json_reader.read()
    593     if should_close:
    594         try:

~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in read(self)
    715             obj = 
    716         else:
--> 717             obj = self._get_object_parser(self.data)
    718         self.close()
    719         return obj

~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in 
_get_object_parser(self, json)
    737         obj = None
    738         if typ == "frame":
 --> 739             obj = FrameParser(json, **kwargs).parse()
    741         if typ == "series" or obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in parse(self)
    848         else:
--> 849             self._parse_no_numpy()
    851         if self.obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in 
   1091         if orient == "columns":
   1092             self.obj = DataFrame(
-> 1093                 loads(json, precise_float=self.precise_float), 
   1094             )
   1095         elif orient == "split":

ValueError: Expected object or value

and I can't understand why. Can somebody please help me? Thank's in advance.

[EDIT] the file is a .jsonl file, but yet I don't know how to import the dataset because I cannot use .read_json ,I have tried this:


but gives me the following error message:

OSError                                   Traceback (most recent call last)
<ipython-input-28-2422c1a9a77b> in <module>
----> 1 openfile=open('Desktop\training_dataset.jsonl')
      2 jsondata=json.load(openfile)
      3 df=pd.DataFrame(jsondata)
      4 openfile.close()
      5 print(df)

OSError: [Errno 22] Invalid argument: 'Desktop\training_dataset.jsonl'

[EDIT 2] by doing as suggested, so:

with open("\Desktop\training_dataset.jsonl") as datafile:
data = json.load(datafile)
dataframe = pd.DataFrame(data)

I again obtain another error message, which is:

OSError                                   Traceback (most recent call last)
<ipython-input-47-1365f26e6db5> in <module>
 ----> 1 with open("\Desktop\training_dataset.jsonl") as datafile:
      2     data = json.load(datafile)
      3 dataframe = pd.DataFrame(data)

OSError: [Errno 22] Invalid argument: '\\Desktop\training_dataset.jsonl'

but I don' understand, because my dataset is placed in my desktop.

  • $\begingroup$ I could reproduce the error when the input file is not a correct json. Did you make sure your input is correct? $\endgroup$ – matthiaw91 Oct 23 '19 at 15:55
  • 1
    $\begingroup$ Given the error message, maybe check this stackoverflow question. $\endgroup$ – matthiaw91 Oct 23 '19 at 19:48

You can follow the below steps to load a json file,

First check whether the file is json or not using the following; https://jsonlint.com/. Once you are confirmed the file is a json, use the below code to read it.

with open("training_dataset.json") as datafile:
    data = json.load(datafile)
dataframe = pd.DataFrame(data)

I hope the above will help you.

| improve this answer | |

A jsonl file is simply a file where each line is a json object. So you can just deserialize each line as a json object and then build a DataFrame

import json
from pandas.io.json import json_normalize

# be sure here that file path is correct
with open("\Desktop\training_dataset.jsonl", 'r', encoding='utf-8') as jsonl_file:
    data = [json.loads(line.strip()) for line in jsonl_file]

dataframe = json_normalize(data)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.