15

I am trying to fetch the JSON data from an url.It is in the form of object i have to push the object data into array.

var my_json;
$.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) {
console.log(json1);
json1.feeds.forEach(function(feed,i){
    console.log("\n The details of " + i + "th Object are :  \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);      
    my_json = feed;
    console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}
    var data = [];
    for(var i in my_json)
    data.push(my_json [i]);
    console.log(data); //["2017-03-14T01:00:32Z", 33358, "4", "4", "0"]
}); 

I have tried as above in my_json var i have the json data in the form of object now i have to store that object in to var data as an array in the below format

[{
        "created_at": "2017-03-14T01:00:32Z",
        "entry_id": 33358,
        "field1": "4",
        "field2": "4",
        "field3": "0"
 },
 {
        "created_at": "2017-03-14T22:54:43Z",
        "entry_id": 33398,
        "field1": "84",
        "field2": "32",
        "field3": "0"
 }];

Can anyone help me how to do it??Thankyou

Anusha
  • 239
  • 1
  • 3
  • 12

4 Answers4

51

Observation

  • If there is a single object and you want to push whole object into an array then no need to iterate the object.

Try this :

var feed = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"};

var data = [];
data.push(feed);

console.log(data);

Instead of :

var my_json = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"};

var data = [];
for(var i in my_json) {
    data.push(my_json[i]);
}

console.log(data);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • I have n no.of objects not a single object@Rohit – Anusha Apr 12 '17 at 06:58
  • 1
    @Anusha But how it is possible. either you can have `array of objects` `[{},{},{}]` or `object of arrays` `{[],[],[]}`. – Debug Diva Apr 12 '17 at 07:03
  • your answer solved my issue tq i made small mistake now its working fine tq.@Rohit – Anusha Apr 12 '17 at 07:07
  • 3
    @RohitJindal how is it possible if I have like this, `dataObj={ "id": 1 , "arr" : [ ] }` and another object like `secondObj = { id: 2 , name: "xy" , area: "area1 " } ` I want to push secondObj to that `arr` inside `dataObj` . Actually I am on TS, it shows type error. – Kavindu Gayantha Nov 27 '20 at 07:37
3
var postdata = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"};

var data = [];
data.push(postdata);

console.log(data);
Alfonso Tienda
  • 3,442
  • 1
  • 19
  • 34
  • 5
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. – 4b0 Jun 25 '19 at 13:20
1

You need to have the 'data' array outside of the loop, otherwise it will get reset in every loop and also you can directly push the json. Find the solution below:-

var my_json;
$.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) {
console.log(json1);
var data = [];
json1.feeds.forEach(function(feed,i){
    console.log("\n The details of " + i + "th Object are :  \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);      
    my_json = feed;
    console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}
    data.push(my_json);
     //["2017-03-14T01:00:32Z", 33358, "4", "4", "0"]
}); 
console.log(data);
Jay
  • 338
  • 1
  • 5
0

can you try something like this. You have to put each json in the data not json[i], because in the way you are doing it you are getting and putting only the properties of each json. Put the whole json instead in the data

 var my_json;
    $.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) {
    console.log(json1);
        var data = [];
    json1.feeds.forEach(function(feed,i){
        console.log("\n The details of " + i + "th Object are :  \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);      
        my_json = feed;
        console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}
        data.push(my_json);
    }); 
strash
  • 1,291
  • 2
  • 15
  • 29