Natasha
Natasha

Reputation: 281

How to convert json array to json object in javascript

I have a json array like in the below image:

JSon array

I want it to be onyl json object and not array. Like when I do JSON.stringify I get this: [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}]

But I only want it like this: {"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}

How do I get that?

UPDATE: Doing this right now already:

var json_temp = JSON.stringify(json4);
    console.log("json_temp")
    console.log(json_temp);
    var json_temp1 = json_temp[0];
    console.log("json temp1");
    console.log(json_temp1);

But getting the following in console.log: Getting this problem

Upvotes: 0

Views: 18177

Answers (1)

Justin Herter
Justin Herter

Reputation: 590

just reference the object like this:

var array = [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}];

var object = array[0];

Alternatively you could copy the object and reassign it to the same variable like the following, note: This second method is a bit more expensive.

var data = [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}];

data = JSON.parse(JSON.stringify(data[0]));

Here is a working example.

Upvotes: 1

Related Questions