简体   繁体   中英

JSON.parse cause unexpected end of json input but my json is correct

i have a python script that create a json and i have a nodejs script that read the json:

  • python script
with open("music.json", "w") as write_file:
    json.dump(music_found, write_file, indent=4)

music_found is an array of object

  • nodejs
import fs from 'fs'
import { cwd } from 'process';


let rawdata = fs.readFileSync(`${cwd()}/music.json`);
let music = JSON.parse(rawdata)
console.log(music);

i get the message unexpected end of json input

  • json example
[
    {
        "user": "some_user1",
        "file": "@@enlbq\\_Music\\Infected Mushroom\\Return to the Sauce [2017] [HMCD94]\\09 - Infected Mushroom - Liquid Smoke.flac",
        "size": 42084572,
        "slots": true,
        "speed": 1003176
    },
    {
        "user": "some_user2",
        "file": "@@xfjpb\\Musiikkia\\Infected Mushroom\\Return to the Sauce\\09 Infected Mushroom - Liquid Smoke.flac",
        "size": 24617421,
        "slots": true,
        "speed": 541950
    },
    {
        "user": "some_user3",
        "file": "@@rxjpv\\MyMusic\\Infected Mushroom\\Infected Mushroom - Return To The Sauce (2017) [CD FLAC]\\09 - Liquid Smoke.flac",
        "size": 41769608,
        "slots": true,
        "speed": 451671
    }
]

my json is well formatted no? i'm on that since 4hours and im stuck on it... very annoying ^^ hope somehone help

readFileSync

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

You need to either set the encoding or use the buffer's .toString( ) method

Can you make sure the path is correct? i can read your data normally in js so i think the problem is just the path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM