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.

7
  • Possible duplicate of Using Node.JS, how do I read a JSON object into (server) memory? Commented Apr 8, 2018 at 17:43
  • Also How to parse JSON using Node.js? Commented Apr 8, 2018 at 17:44
  • I have tried all those methods but i still get the type string for unknown reason. Commented Apr 8, 2018 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. Commented Apr 8, 2018 at 17:47
  • 1
    Just use require('./data/users.json'). Commented Apr 8, 2018 at 18:01

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

I get it now but when i use the other way around i get this error: undefined: 1 '{' SyntaxError: Unexpected token in JSON at position 0
can you print the string you received from readFileSync using console.info and check if that's correct?
Yes everything looks fine. But cant get rid of this error.
When i print the string in the console i dont receive with the quotes at the ends is that something that i should worry about @S4beR ?
i assume you are seeing something like {"key": "value"}? This representation is fine
|
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

3 Comments

I dont know but i get the error undefined: 1 '{' SyntaxError: Unexpected token in JSON at position 0
just to make sure, you have a folder called data and inside has users.json right?
it stays with the same problem @AmirHosseinRd
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);

1 Comment

i really dont know what I'm doing wrong maybe it is from the json that is not constructed well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.