0

So I got this JSON Array in a variable named jsonObject:

    jsonObject = {
    "log": [{
        "date": "15/09/2016",
        "time": "15:35:56",
        "temp": "16.0",
        "humidity": "95.0"
    }, {
        "date": "15/09/2016",
        "time": "15:35:59",
        "temp": "30.0",
        "humidity": "61.0"
    }, {
        "date": "15/09/2016",
        "time": "15:36:03",
        "temp": "30.0",
        "humidity": "60.0"
    }]
}

My goal is to iterate over it and place the data in a table and to do this I want the JSON Array as a normal array in Javascript. I have found many code examples on this, but none of them take into account the name of the array in this case "log". Anyone know how I can get rid of the name and just get an array? I could make the JSON array itne a string, substring it and then convert it into a JSON Array again and then convert it to a Array, but it feels very ineffeficent. Perhaps there is a way to create a 2D array of the JSON Array as a string but I don't know how.

7
  • 1
    JSON Commented Sep 15, 2016 at 14:25
  • How EXACTLY does your jsonObject look like ... more like jsonObject = {...} or more like jsonObject = "{...}"??? Commented Sep 15, 2016 at 14:26
  • It looks exactly like the code above, I will edit it now. Commented Sep 15, 2016 at 14:29
  • What does console.log(typeof jsonObject) give you? If it is string then it is JSON, if object then it's a plain old Javascript object. Commented Sep 15, 2016 at 14:31
  • 1
    Re edit: benalman.com/news/2010/03/theres-no-such-thing-as-a-json — There is absolutely no JSON in your question. Commented Sep 15, 2016 at 14:32

2 Answers 2

1

JSON objects are javascript variables. If you want log, just grab log.

jsonObject = {
    "log": [{
        "date": "15/09/2016",
        "time": "15:35:56",
        "temp": "16.0",
        "humidity": "95.0"
    }, {
        "date": "15/09/2016",
        "time": "15:35:59",
        "temp": "30.0",
        "humidity": "61.0"
    }, {
        "date": "15/09/2016",
        "time": "15:36:03",
        "temp": "30.0",
        "humidity": "60.0"
    }]
}
logArray = jsonObject.log
Sign up to request clarification or add additional context in comments.

3 Comments

"JSON objects are javascript variables" — They are not. benalman.com/news/2010/03/theres-no-such-thing-as-a-json
in this case he is right because the OP said that it's a JSON Array in a variable named jsonObject
If you omit the first sentence (or rewrite it to something like You already have an object literal called jsonObject) this should be the accepted answer
0

If you want to iterate over the array, try something like this:

var jsonObj = {
        "log": [{
            "date": "15/09/2016",
            "time": "15:35:56",
            "temp": "16.0",
            "humidity": "95.0"
        }, {
            "date": "15/09/2016",
            "time": "15:35:59",
            "temp": "30.0",
            "humidity": "61.0"
        }, {
            "date": "15/09/2016",
            "time": "15:36:03",
            "temp": "30.0",
            "humidity": "60.0"
        }]
    };


 var jsonArr = jsonObj['log'];

 for(var i in jsonArr){
      console.log(JSON.stringify(jsonArr[i]));
 }

2 Comments

This gives me each row, how about if I want a specific value, say I'm looking for the key "time" and it's value "15:35:59" It's mostly the value I am interested in.
Instead of jsonArr[i], include the key also like this : console.log(jsonArr[i]['time']);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.