1

This must be something really silly. After spending the last few hours, I am here for help. I have a users.json file

{
    "Test_Session": {
        "test_SessionID": [
            {
                "$": {
                    "id": "1"
                },
                "test_type": [
                    "1"
                ],
                "Test_IDtest": [
                    "1"
                ],
                "DataURL": [
                    "data1"
                ]
            }
        ]
    }
}

I try to read DataURL by

 var jsonData = require('./users.json');
var test = JSON.stringify(jsonData)
console.log(test.Test_Session.test_SessionID.DataURL);  

In console, I get "Can't read property test_SessionID of undefined".

What's going on?

2 Answers 2

2

Your main issue is that test_SessionID is an array, so when you try to access DataUrl, it will be undefined. You need to select the index of the test_SessionID object you want to read from. Try this:

console.log(test.Test_Session.test_SessionID[0].DataURL);

Also, you don't need to JSON.stringify anything, Node automatically reads the file in as JSON, so just doing

var jsonData = require('./users.json');
console.log(jsonData.Test_Session.test_SessionID[0].DataURL);

should work fine.

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

1 Comment

Glad to help :)
1

Node is already interpreting the JSON, try the following:

var test = require('./users.json');
console.log(test.Test_Session.test_SessionID[0].DataURL);

Comments

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.