1

Given a json file test.json with the content as follows:

{'review/appearance': 2.5, 'beer/style': 'Hefeweizen', 'review/palate': 1.5, 'review/taste': 1.5, 'beer/name': 'Sausa Weizen', 'review/timeUnix': 1234817823, 'beer/ABV': 5.0, 'beer/beerId': '47986', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 16, 'hour': 20, 'min': 57, 'sec': 3, 'mon': 2, 'year': 2009, 'yday': 47, 'wday': 0}, 'review/overall': 1.5, 'review/text': 'A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.', 'user/profileName': 'stcules', 'review/aroma': 2.0}
{'review/appearance': 3.0, 'beer/style': 'English Strong Ale', 'review/palate': 3.0, 'review/taste': 3.0, 'beer/name': 'Red Moon', 'review/timeUnix': 1235915097, 'beer/ABV': 6.2, 'beer/beerId': '48213', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 1, 'hour': 13, 'min': 44, 'sec': 57, 'mon': 3, 'year': 2009, 'yday': 60, 'wday': 6}, 'review/overall': 3.0, 'review/text': 'Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.', 'user/profileName': 'stcules', 'review/aroma': 2.5}
{'review/appearance': 3.0, 'beer/style': 'Foreign / Export Stout', 'review/palate': 3.0, 'review/taste': 3.0, 'beer/name': 'Black Horse Black Beer', 'review/timeUnix': 1235916604, 'beer/ABV': 6.5, 'beer/beerId': '48215', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 1, 'hour': 14, 'min': 10, 'sec': 4, 'mon': 3, 'year': 2009, 'yday': 60, 'wday': 6}, 'review/overall': 3.0, 'review/text': 'Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.', 'user/profileName': 'stcules', 'review/aroma': 2.5}

I have load the file with the following code:

import json
with open('./test.json', 'r') as json_file:
    data = json.load(json_file)

But it raises an error: JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1).

Another option:

data = json.loads('./test.json')
print(data)

Output:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How could I do it correctly? Many thanks at advance.

ah bon
  • 9,293
  • 12
  • 65
  • 148
  • 3
    Does this answer your question? [Python/Json:Expecting property name enclosed in double quotes](https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes) – TheSavageTeddy Nov 02 '20 at 06:03

4 Answers4

2

There are 2 issues in JSON.

  1. Array brackets are missing []
  2. Modify ' to "

Correct JSON:

[{
    "review/appearance": 2.5,
    "beer/style": "Hefeweizen",
    "review/palate": 1.5,
    "review/taste": 1.5,
    "beer/name": "Sausa Weizen",
    "review/timeUnix": 1234817823,
    "beer/ABV": 5.0,
    "beer/beerId": "47986",
    "beer/brewerId": "10325",
    "review/timeStruct": {
        "isdst": 0,
        "mday": 16,
        "hour": 20,
        "min": 57,
        "sec": 3,
        "mon": 2,
        "year": 2009,
        "yday": 47,
        "wday": 0
    },
    "review/overall": 1.5,
    "review/text": "A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.",
    "user/profileName": "stcules",
    "review/aroma": 2.0
}, {
    "review/appearance": 3.0,
    "beer/style": "English Strong Ale",
    "review/palate": 3.0,
    "review/taste": 3.0,
    "beer/name": "Red Moon",
    "review/timeUnix": 1235915097,
    "beer/ABV": 6.2,
    "beer/beerId": "48213",
    "beer/brewerId": "10325",
    "review/timeStruct": {
        "isdst": 0,
        "mday": 1,
        "hour": 13,
        "min": 44,
        "sec": 57,
        "mon": 3,
        "year": 2009,
        "yday": 60,
        "wday": 6
    },
    "review/overall": 3.0,
    "review/text": "Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.",
    "user/profileName": "stcules",
    "review/aroma": 2.5
}, {
    "review/appearance": 3.0,
    "beer/style": "Foreign / Export Stout",
    "review/palate": 3.0,
    "review/taste": 3.0,
    "beer/name": "Black Horse Black Beer",
    "review/timeUnix": 1235916604,
    "beer/ABV": 6.5,
    "beer/beerId": "48215",
    "beer/brewerId": "10325",
    "review/timeStruct": {
        "isdst": 0,
        "mday": 1,
        "hour": 14,
        "min": 10,
        "sec": 4,
        "mon": 3,
        "year": 2009,
        "yday": 60,
        "wday": 6
    },
    "review/overall": 3.0,
    "review/text": "Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.",
    "user/profileName": "stcules",
    "review/aroma": 2.5
}] 
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
1

this is not json file. Looks like ndjson look at third-party package ndjson

Also, it should be using double quotes, not single quotes.

If this is a file that you create or can change - you can put these 3 JSON objects into JSON array and then t will be valid JSON.

buran
  • 13,682
  • 10
  • 36
  • 61
1

The problem is that your file is not valid JSON. You need double quotes instead of single quotes around your strings, and you need to enclose your three objects inside an array.

Try this instead:

[
    {"review/appearance": 2.5, "beer/style": "Hefeweizen", "review/palate": 1.5, "review/taste": 1.5, "beer/name": "Sausa Weizen", "review/timeUnix": 1234817823, "beer/ABV": 5.0, "beer/beerId": "47986", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 16, "hour": 20, "min": 57, "sec": 3, "mon": 2, "year": 2009, "yday": 47, "wday": 0}, "review/overall": 1.5, "review/text": "A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.", "user/profileName": "stcules", "review/aroma": 2.0},
    {"review/appearance": 3.0, "beer/style": "English Strong Ale", "review/palate": 3.0, "review/taste": 3.0, "beer/name": "Red Moon", "review/timeUnix": 1235915097, "beer/ABV": 6.2, "beer/beerId": "48213", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 1, "hour": 13, "min": 44, "sec": 57, "mon": 3, "year": 2009, "yday": 60, "wday": 6}, "review/overall": 3.0, "review/text": "Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.", "user/profileName": "stcules", "review/aroma": 2.5},
    {"review/appearance": 3.0, "beer/style": "Foreign / Export Stout", "review/palate": 3.0, "review/taste": 3.0, "beer/name": "Black Horse Black Beer", "review/timeUnix": 1235916604, "beer/ABV": 6.5, "beer/beerId": "48215", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 1, "hour": 14, "min": 10, "sec": 4, "mon": 3, "year": 2009, "yday": 60, "wday": 6}, "review/overall": 3.0, "review/text": "Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.", "user/profileName": "stcules", "review/aroma": 2.5}
]

By the way, obj = json.loads('./test.json') will not work, as json.loads expects a JSON string, not a file path or file object. (loads is short for "load string".) You want json.load instead.

ah bon
  • 9,293
  • 12
  • 65
  • 148
Jack Taylor
  • 5,588
  • 19
  • 35
  • How could I manipulate the file into standard json format? the real data is quite large, which has 50000 entries. – ah bon Nov 02 '20 at 06:14
  • BTW, I get an error `AttributeError: 'str' object has no attribute 'read'` with `json.load`. – ah bon Nov 02 '20 at 06:17
  • @ahbon You have two options. 1) Find what format the file was saved as and find a tool that reads that format. 2) Convert the file into valid JSON using a script. – Jack Taylor Nov 02 '20 at 06:24
  • @ahbon The AttributeError sounds like you are trying to use the `read()` method on a string, instead of on a file object. – Jack Taylor Nov 02 '20 at 06:25
0

The file is not json. This has been asked before here

JSON Does not use single quote ', they instead use double quotes "

TheSavageTeddy
  • 204
  • 1
  • 13