0

i have a Json Object in Javascript, looking like this:

{
"properties": {
    "processowner" : { "valueType": "text" },
    "withwhat" : { "valueType": "text" },
    "withwho" : { "valueType": "text" },
    "processstep" : { "valueType": "text" },
    "cluster" : { "valueType": "text" }
},
"items": [
    {
        "label": "anytitle",
        "processowner": ["Name"],
        "withwhat": ["ben\u00f6tigtes Material","ben\u00f6tigtes Material2","ben\u00f6tigtes Material3"],
        "withwho": ["Verantwortliche Person"],
        "processstep": ["1. Schritt 1","2. Schritt 2","3. Schritt 3","4. Schritt 4","5. Schritt 5"],
        "cluster": ["anyname"]
    },
    {
        "label": "anytitle",
        "processowner": ["Name"],
        "withwhat": ["ben\u00f6tigtes Material","ben\u00f6tigtes Material2","ben\u00f6tigtes Material3"],
        "withwho": ["Verantwortliche Person"],
        "processstep": ["1. Schritt 1","2. Schritt 2","3. Schritt 3","4. Schritt 4","5. Schritt 5"],
        "cluster": ["anyname"]
    },
........
}

What i want is only the "items" part. Looking like this:

[
    {
        "label": "anytitle",
        "processowner": ["Name"],
        "withwhat": ["ben\u00f6tigtes Material","ben\u00f6tigtes Material2","ben\u00f6tigtes Material3"],
        "withwho": ["Verantwortliche Person"],
        "processstep": ["1. Schritt 1","2. Schritt 2","3. Schritt 3","4. Schritt 4","5. Schritt 5"],
        "cluster": ["anyname"]
    },
    {
        "label": "anytitle",
        "processowner": ["Name"],
        "withwhat": ["ben\u00f6tigtes Material","ben\u00f6tigtes Material2","ben\u00f6tigtes Material3"],
        "withwho": ["Verantwortliche Person"],
        "processstep": ["1. Schritt 1","2. Schritt 2","3. Schritt 3","4. Schritt 4","5. Schritt 5"],
        "cluster": ["anyname"]
    },
........
]

I tried to access it with objectname.items But i always get "undefined" when printing out on console.

edit:

function getJSON() {
var req = https.get(options, function(response) {
      // handle the response
      var res_data = '';
      response.on('data', function(chunk) {
        res_data += chunk;
      });
      response.on('end', function() {
            var wikiJSON = res_data;
            console.log(wikiJSON.items);
      });
    });
    req.on('error', function(e) {
      console.log("Got error: " + e.message);
    });
    req.end();              

}

dnks23
  • 359
  • 6
  • 22
  • 1
    Well how do you get the object? How are you referencing it? You need to provide more details. – epascarello Oct 26 '15 at 19:05
  • can you show us how you are accessing it now? – doogle Oct 26 '15 at 19:05
  • 2
    `i have a Json Object`, JSON is a string representation of an object. So do you actually have a string? Or do you have a javascript object? If you have an object, then `obj.items` should get you exactly what you need. – Matt Burland Oct 26 '15 at 19:06
  • How are you referencing the object you are getting? – Shubham Aggarwal Oct 26 '15 at 19:07
  • i am getting the json as showed above from an http.get to an api.... the data receiving is saved in an object. If i do console.log(myobj) i am getting the json as showed above(first code block). Do i need to convert that data before saving it to an object? – dnks23 Oct 26 '15 at 19:09
  • 1
    Show the code of ajax call, and the code where you are accessing it – vinayakj Oct 26 '15 at 19:11
  • 1
    @dnks23: Is it a string or an object? Try `typeof(myObj)`, if it returns `string`, then that's your problem. You'd need to parse it first with `JSON.parse(myobj)` and then you can access the `items` property of that object. Depending on how and what you are using for the `get`, it may be possible for it to be parsed automatically (jquery, for example, will do this if you set up the request right). – Matt Burland Oct 26 '15 at 19:13
  • ok thanks guys. json.parse(myobj) did the trick!! thank @MattBurland burland – dnks23 Oct 26 '15 at 19:17

0 Answers0