0

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

bwanlin
  • 1
  • 1

2 Answers2

1

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

  • when i use the nodejs script it works but when i call it from python with subprocess.call its doesnt and i have the unexpected end of json input... (i do that because i don't find a equivalent npm package for python) – bwanlin Aug 21 '20 at 18:58
0

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

Ando
  • 375
  • 2
  • 8
  • the path is correct when i console.log it, i don't have to use cwd tho but try something since im stuck, u can read it normally ? pls tell me what do you do ^^ – bwanlin Aug 20 '20 at 15:09