user3335607 user3335607 - 1 year ago 68
Javascript Question

Access nested members in JSON

I'm trying to access members in a json, however I am running into some trouble. Here is an example of one of the json objects, stored in var obj:

var fs = require('fs');
var obj = [
"_id": "52d7f816f96d7f6f31fbb680",
"regNum": "0361300035313000002",
"sd": "2013-01-01T00:00:00",
"pd": "2013-01-25T09:30:29Z",
"prd": "2012-12-18",
"p": 1395000000,
"pt": [
"name": name here",
"price": 1395000000,
"OKDP": {
"code": "5520109",
"name": "name here"
"sid": "25484812",
"sum": "1395000000",
"OKEI": {
"code": "796",
"name": "name two"
"quantity": "1"
"b": 0,
"c": 0,
"s": 0

I'm trying to access the sid and sum values, by doing the following:

var sid = [];
var sum = [];

obj.forEach(block => {
var sidOut =;
var sumOut =;




I tried the solution here, however, when I run these it gives me [ undefined ] errors.

Why am I unable to access this two values?

Answer Source

if you see your pt is an array of an object [{}] so you need to select which element you want to access so

var sidOut =[0].sid;
var sumOut =[0].sum;

should get you the right result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download