0

i have a json-file looking like this:

 { 
   "results": {
    "name1": {
        "printouts": {
            "property1": [{
                    "someName": "someText",
                    "someName2": "someURL",
                    "someName3": integer,
                    "someName4": boolean
                }
            ],
            "property2": [
                "someText"
            ],
            "property3": [
                "someText"
            ],
            "property4": [
                "someText"
            ],
            "property5": [
                "someText"
            ],
            "property6": [
                "someText"
            ]
        },
        "someName": "someText",
        "someName2": "someURL",
        "someName3": integer,
        "someName4": boolean
    },
    "name2": {
        "printouts": {
            "property1": [{
                    "fulltext": "someText",
                    "fullurl": "someURL",
                    "namespace": integer,
                    "exists": boolean
                }
            ],
            "property2": [
                "someText"
            ],
            "property3": [
                "someText"
            ],
            "property4": [
                "someText"
            ],
            "property5": [
                "someText"
            ],
            "property6": [
                "someText"
            ]
        },
        "someName": "someText",
        "someName2": "someURL",
        "someName3": integer,
        "someName4": boolean
    },
 ......
}

What i want is to access property6 of each object in printouts. What i've done so far:

 for (i in results) {
        if (someOtherArray.length < 1) {
            someOtherArray.push(i.printouts.property6.toString());
        }

But this gets me Error: Cannot read property property6 of undefined. What am i doing wrong?

Any Idea? Thanks.

11
  • it may be i.name.printouts.property6.toString() Commented Jan 5, 2017 at 12:04
  • results should be an array, instead of an object, because of the double name property. Commented Jan 5, 2017 at 12:04
  • Looks like you have an extra " in there after someText"property5": [someText"], Commented Jan 5, 2017 at 12:04
  • i will hold key name in string. You will have to use results[i] to get object Commented Jan 5, 2017 at 12:05
  • 1
    @dnks23, but you have twice name keys inside. Commented Jan 5, 2017 at 12:09

1 Answer 1

2

Basically you have the wrong access to the property with

i.printouts.property6

it needs to be an object with a property accessor with bracket notation for a variable.

data.results[i].printouts.property6
//^^^^^^^^^^^^^

Then you could iterate over the keys and select the property you want.

var data = { results: { name1: { printouts: { property1: [{ someName: "someText", someName2: "someURL", someName3: 33, someName4: false }], property2: ["someText"], property3: ["someText"], property4: ["someText"], property5: ["someText"], property6: ["someText"] }, someName: "someText", someName2: "someURL", someName3: 37, someName4: true }, name2: { printouts: { property1: [{ fulltext: "someText", fullurl: "someURL", namespace: 35, "exists": true }], property2: ["someText"], property3: ["someText"], property4: ["someText"], property5: ["someText"], property6: ["someText"] }, someName: "someText", someName2: "someURL", someName3: 34, someName4: false } } };

Object.keys(data.results).forEach(function (k) {
    console.log(data.results[k].printouts.property6);
});

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.