J
J
jekanok2018-01-27 14:53:44
Node.js
jekanok, 2018-01-27 14:53:44

How to parse json file in node js?

Here is the part of the json file where I need to take for example name and op so I write like this

var fs = require("fs");
var contents = fs.readFileSync("priceData.json");
 var jsonContent = JSON.parse(contents);
 console.log(jsonContent.name);

and gives me
PS C:\Users\Eugene\Desktop\steam-trade> node bot
undefined

{ name: 'AK-47 | Red Laminate (Field-Tested)',
    s7d: [ 1030, 59 ],
    op: [ 671, 255 ],
    bs: [ 699, 72 ],
    bsb: [ 594 ],
    tm: [ 429, 81 ],
    JARD: [ 0, 0 ],
    roll: [ 1015, 31 ],
    csm: [ 1125, 4 ],
    css: [ 0, 0 ],

    csw: [ 0, 0 ],
    ts: [ 1040, 2 ],
    jar: [ 0, 0 ],
    me: [ 0, 0 ],
    gg: [ 0, 0 ],
    rt: [ 0, 0 ],
    skts: [ 0, 0 ],
    de: [ 0, 0 ],
    su: [ 0, 0 ],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ro37A, 2018-01-27
@Sayto

Your JSON is not valid.
In JSON, all keys are strings.

{ "name": "AK-47 | Red Laminate (Field-Tested)",
    "s7d": [ 1030, 59 ],
    "op": [ 671, 255 ],
    ...
}

I
Interface, 2018-01-27
@Interface

try
fs.readFileSync("priceData.json").toString()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question