-4

I have tried to load a JSON file in pandas with this code and I have this error in the fist line. I think that same thing is wrong in json structure because I tried also with pd.read_json and it didn't work. What is wrong here?

Name_file='data582750.txt'
l=[]
with open(Name_file) as f:
    for line in f:
        l.append(line)
data = json.loads(l)
json_normalize(data)

I get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-c050037c7cb9> in <module>()
----> 1 data = json.loads(l)
C:\Users\demighaa\AppData\Local\Continuum\Anaconda2\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)

    337             parse_int is None and parse_float is None and
    338             parse_constant is None and object_pairs_hook is None and not kw):
--> 339         return _default_decoder.decode(s)
    340     if cls is None:
    341         cls = JSONDecoder

C:\Users\demighaa\AppData\Local\Continuum\Anaconda2\lib\json\decoder.pyc in decode(self, s, _w)
    362 
    363         """
--> 364         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    365         end = _w(s, end).end()
    366         if end != len(s): 

TypeError: expected string or buffer

The first line in the file is :

'{"Operator":0,"Device":"F0FCC","Time":1494854190,"FormattedTime":"2017-05-15 15:16:30 +0200 CEST","Data":"042911e3c78f2193262c58b2","JSONDecodedData":"{"Service":{"S":{"Typ":0,"Version":2,"Vdd":0},"Cpt":2,"Start":0},"ExtTemperature":[22.25,21.5,21.5,21.5,21.5,22],"Moisture":[50,50,49,49,49,50],"IntTemperature":[0,0,0,0,0,0,0,0,0]}","Snr":"48.21","ComputedLocation":{"Lat":0,"Lng":0},"LinkQuality":"EXCELLENT"}\n'
Ajean
  • 5,528
  • 14
  • 46
  • 69
  • 5
    Don't post code as an image - put a [MCVE] in your post that people can copy out (as text) to check and edit. – asongtoruin May 16 '17 at 11:26
  • 1
    Props for removing the image and adding text, but you should try to format the code correctly to get help - it's difficult to see what's going on here. – samiles May 16 '17 at 12:37
  • your issue is unreadable... you should take the time edit you post to improve indentation!! just a simple button `{}` to auto indent.... – MTroy May 16 '17 at 15:08

3 Answers3

2

Please post code, not images...

Anyway: from what I can see you have a jsonlines file - each line is a the representation of a distinct json object by itself. json.loads() expects a single string (representing a single json object), not a list. So the obvious solution is decode the json lines on the fly instead of passing the whole list, ie:

data = []
with open("you/file.ext") as f:
    for line in f:
        data.append(json.loads(line))
json_normalize(data)

Edit: given your comment you may not have a jsonlines file. If it's a real json file then just pass the whole file content to json.loads() or, even simpler, pass the file itself to json.load(). And do the world a favour: read that freaking manual before posting, it will save everyone's time :-/

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

Should be json.loads(l[0]) judging by your picture... The json.loads function only takes string or buffer but you're providing a list

moritzg
  • 4,266
  • 3
  • 37
  • 62
0

May be there is some problem with JSON data! Validate the data using a JSON validator. You can just copy-paste date on jsonlint and check if JSON is properly formatted or not.

Shashank
  • 584
  • 5
  • 16