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

10
  • 1
    Do JSON.stringify(myArray[0])? Commented Oct 28, 2016 at 15:56
  • JSON.parse(array[0]) 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 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"} 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. Commented Oct 28, 2016 at 16:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Please see my update above. I'm doing that already but getting weird output on console.log
@Natasha your data is an array of arrays and thats why its not working for you. You have multiple datasets coming in so what do you want to do with them?
I have update the answer with two working examples hope this helps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.