Home > Mobile >  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) from a local file
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) from a local file

Time:10-20

I am trying to use a json file to populate my database, but when I use

from json import loads

with open(path.join(path.abspath("..\Data"), "monsters.json" ) as f:
        print(f.read())
        json_file = loads(f.read())

I get the aforementioned error. What I understand about this error is that it says that what I am trying to pass through is either does not use proper json syntax, or does not have any dat within the input.

here is a code snippet of the json file:

[
  {
    "name": "Aboleth",
    "meta": "Large aberration, lawful evil",
    "ac": "17 (Natural Armor)",
    "hp": "135 (18d10   36)",
    "Speed": "10 ft., swim 40 ft. ",
    "STR": "21",
    "strMod": "( 5)",
    "DEX": "9",
    "dexMod": "(-1)",
    "CON": "15",
    "conMod": "( 2)",
    "INT": "18",
    "intMod": "( 4)",
    "WIS": "15",
    "wisMod": "( 2)",
    "CHA": "18",
    "chaMod": "( 4)",
    "savingThrow": "CON  6, INT  8, WIS  6",
    "Skills": "History  12, Perception  10",
    "Senses": "Darkvision 120 ft.,  Passive Perception 20",
    "Languages": "Deep Speech, Telepathy 120 ft.",
    "Challenge": "10 (5,900 XP)",
    "Traits": "<p><em><strong>Amphibious.</strong></em> The aboleth can breathe air and water. </p><p><em><strong>Mucous Cloud.</strong></em> While underwater, the aboleth is surrounded by transformative mucus. A creature that touches the aboleth or that hits it with a melee attack while within 5 feet of it must make a DC 14 Constitution saving throw. On a failure, the creature is diseased for 1d4 hours. The diseased creature can breathe only underwater. </p><p><em><strong>Probing Telepathy.</strong></em> If a creature communicates telepathically with the aboleth, the aboleth learns the creature's greatest desires if the aboleth can see the creature.</p>",
    "Actions": "<p><em><strong>Multiattack.</strong></em> The aboleth makes three tentacle attacks. </p><p><em><strong>Tentacle.</strong></em> <em>Melee Weapon Attack:</em>  9 to hit, reach 10 ft., one target. <em>Hit:</em> 12 (2d6   5) bludgeoning damage. If the target is a creature, it must succeed on a DC 14 Constitution saving throw or become diseased. The disease has no effect for 1 minute and can be removed by any magic that cures disease. After 1 minute, the diseased creature's skin becomes translucent and slimy, the creature can't regain hp unless it is underwater, and the disease can be removed only by heal or another disease-curing spell of 6th level or higher. When the creature is outside a body of water, it takes 6 (1d12) acid damage every 10 minutes unless moisture is applied to the skin before 10 minutes have passed. </p><p><em><strong>Tail.</strong></em> <em>Melee Weapon Attack:</em>  9 to hit, reach 10 ft. one target. <em>Hit:</em> 15 (3d6   5) bludgeoning damage. </p><p><em><strong>Enslave (3/Day).</strong></em> The aboleth targets one creature it can see within 30 feet of it. The target must succeed on a DC 14 Wisdom saving throw or be magically charmed by the aboleth until the aboleth dies or until it is on a different plane of existence from the target. The charmed target is under the aboleth's control and can't take reactions, and the aboleth and the target can communicate telepathically with each other over any distance. </p><p>Whenever the charmed target takes damage, the target can repeat the saving throw. On a success, the effect ends. No more than once every 24 hours, the target can also repeat the saving throw when it is at least 1 mile away from the aboleth.</p>",
    "legendaryActions": "<p>The aboleth can take 3 legendaryActions, choosing from the options below. Only one legendary action option can be used at a time and only at the end of another creature's turn. The aboleth regains spent legendaryActions at the start of its turn. </p><p><em><strong>Detect.</strong></em> The aboleth makes a Wisdom (Perception) check. </p><p><em><strong>Tail Swipe.</strong></em> The aboleth makes one tail attack. </p><p><em><strong>Psychic Drain</strong></em> (Costs 2 Actions). One creature charmed by the aboleth takes 10 (3d6) psychic damage, and the aboleth regains hp equal to the damage the creature takes.</p>",
    "image": "https://media-waterdeep.cursecdn.com/avatars/thumbnails/0/11/1000/1000/636238825975375671.jpeg"
  },
...
]

Now one thing I have tried was to sanitize the data using this:

with open(path.join(path.abspath("..\Data"), "monsters.json" ), encoding='utf-8') as f:
        print(f.read().strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))
        json_file = loads(f.read().strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))

But to no luck.

Then I tried just using load(f), but I still receive the same error.

Is there something I am missing? Thanks for the help regardless!

EDIT: As per comments request, I edited the json code snippet to include not just the first ten lines but the first entry.

I also used the read snippet to be:

with open(path.join(path.abspath("..\Data"), "monsters.json" ), encoding='utf-8') as f:
        json_file = loads(f.read()..strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))

and it give me a new error: json.decoder.JSONDecodeError: Expecting ',' delimiter: line 25 column 604 (char 1244)

and so when looking at this... I think the parser is not reading the traits entry correctly, but I don't know why.

CodePudding user response:

Most likely, since the error points to the first character in the file, the data starts with a BOM since otherwise the data is correct if the ,... at the end is removed. Use the following to remove the BOM if present in UTF-8 data:

import json

with open('monsters.json', encoding='utf-8-sig') as f:
    data = json.load(f)

Also worth noting that at least on Python 3.9 the error is improved if utf-8-sig is not used:

json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
  • Related