Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push JSON object in to array using javascript

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

like image 407
Anusha Avatar asked Apr 12 '17 06:04

Anusha


People also ask

Can you push an object into an array JavaScript?

To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr. push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.

How do you push items into an array?

JavaScript Array push()The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.

How do you add an object to a JSON array in Java?

Java For Testers It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

Can we convert JSON to array?

Convert JSON to Array Using `json. The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.


3 Answers

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);
like image 124
Creative Learner Avatar answered Oct 18 '22 21:10

Creative Learner


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);
like image 2
Midhi Naveen Avatar answered Oct 18 '22 23:10

Midhi Naveen


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);
like image 1
Jay Avatar answered Oct 18 '22 22:10

Jay