最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to convert json array to json object in javascript - Stack Overflow

programmeradmin4浏览0评论

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

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

Share edited Oct 28, 2016 at 16:40 Natasha asked Oct 28, 2016 at 15:53 NatashaNatasha 2812 gold badges5 silver badges12 bronze badges 10
  • 1 Do JSON.stringify(myArray[0])? – George Commented Oct 28, 2016 at 15:56
  • JSON.parse(array[0]) – Ram Commented Oct 28, 2016 at 16:00
  • @ram Please see my update above. I'm doing that already but getting weird output on console.log – Natasha Commented Oct 28, 2016 at 16:13
  • 1 @JustinHerter there will never be any more sets of data other that what I've shown. Data will always be in the form (only values will differ): [{"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"}] and I want it to be in the form: {"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"} – Natasha Commented Oct 28, 2016 at 16:29
  • 1 I never changed the question. I was already doing what the answer says, but that isn't working. I still want to extract json object from the json array. – Natasha Commented Oct 28, 2016 at 16:41
 |  Show 5 more ments

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论