1

I have a .json file where i have people's names stored. I'm reading the content from this file using the file system from Node Manager and then I'm trying to convert this json to string and parsing it to JS object. After parsing it to JS object i get as type string instead of object. Here is the example json file :

{
"21154535154122752": {
    "username": "Stanislav",
    "discriminator": "0001",
    "id": "21154535154122752",
    "avatar": "043bc3d9f7c2655ea2e3bf029b19fa5f",
    "shared_servers": [
        "Reactiflux",
        "Discord Testers",
        "Official Fortnite",
        "Discord API"
    ]
    }
}

and here is the code for processing the data:

const string_data = JSON.stringify(fs.readFileSync('data/users.json', 'utf8'));
const data = JSON.parse(string_data);
console.log(typeof(data)); // <-- this line here shows the type of data as string
const results_array   = Object.values(data);

where fs is the file system package from npm.

  • Possible duplicate of [Using Node.JS, how do I read a JSON object into (server) memory?](https://stackoverflow.com/questions/10011011/using-node-js-how-do-i-read-a-json-object-into-server-memory) – Heretic Monkey Apr 08 '18 at 17:43
  • Also [How to parse JSON using Node.js?](//stackoverflow.com/q/5726729) – Heretic Monkey Apr 08 '18 at 17:44
  • I have tried all those methods but i still get the type string for unknown reason. – Peter Stoyanov Apr 08 '18 at 17:45
  • I would recommend to do a console.log() after each statement and see its format, and so its easier to understand where and whats going wrong. also assign fs.readFileSync to a variable and then reuse it. its more readable IMO. – AmirHossein Rd Apr 08 '18 at 17:47
  • if i use this line of code var data = JSON.parse(fs.readFileSync('data/users.json', 'utf8')); i get undefined : 1 { – Peter Stoyanov Apr 08 '18 at 17:47
  • I need to use JSON stringify at all costs because the JSON parse doesnt accept the json but when i use it i dont get the object from JSON parse – Peter Stoyanov Apr 08 '18 at 17:49
  • 1
    Just use `require('./data/users.json')`. –  Apr 08 '18 at 18:01

3 Answers3

1

Okay so fs.readFileSync returns a string so you dont need to use stringify

var fs = require('fs');
var read = fs.readFileSync('data/users.json', 'utf8');
console.log(read);
console.log(typeof(read));
const data = JSON.parse(read);
console.log(typeof(data));

You will see it returns an object

AmirHossein Rd
  • 393
  • 4
  • 13
1

don't use JSON.stringify as it is further changing the string representation of JSON object. An example of what is happening is below

Imagine if you have a data in your file as shown below

{
   "key": "value"
}

When you read the file (using readFileSync) and apply JSON.stringify, it is converted to a new string as shown below. You can notice that the double quotes are now escaped

"{\"key\": \"value\"}"

Now when you will parse it using JSON.parse then instead of getting the desired object, you are going to get back the same string you read from the file.

You are basically first performing and then undoing the stringify operation

S4beR
  • 1,872
  • 1
  • 17
  • 33
0

This works for me:

const data = JSON.parse(fs.readFileSync('data/users.json', 'utf8'));
console.log(typeof(data)); // <-- this line here shows the type of data as OBJECT
const results_array   = Object.values(data);
Honza Hejzl
  • 874
  • 8
  • 23