-4

I am looking for some solution for my following question. I get the following JSON (sample one).

[{"id":1, "name":firstname}, {"id":2, "name":secondname}]  

But I need {"id":1, "name":firstname}, {"id":2, "name":secondname}
I tried with parse, stringify, replace, slice. But there is no response like what I need. I need to remove that array symbol []. Can anyone suggest a idea or solution? Thanks.

Prabha
  • 29
  • 7
  • Check this link maybe possible for duplicate or it may help you - https://stackoverflow.com/questions/5310304/remove-json-element – Prabaharan Rajendran Mar 15 '18 at 07:55
  • I get you want to have single elements instead of an Array, but for what purpose. Maybe it would be more efficient to work with an Array and handle the seperation in code behind. – Vulpex Mar 15 '18 at 07:56

2 Answers2

0

Considering that you are working with strings:

var s = JSON.stringify([{"id":1, "name":"firstname"}, {"id":2, "name":"secondname"}])
var result = s.substring(1, s.length - 1)

Given that I'm not sure you really need something like that.

ema
  • 5,668
  • 1
  • 25
  • 31
0
var s = JSON.stringify({"str":[{"id":1, "name":"firstname"}, {"id":2, "name":"secondname"}]});
var result = s.substring(8, s.length-2);
result = result.toString();
Santosh Singh
  • 561
  • 2
  • 16
  • 2
    While this code snippet might answer the question it is always better to provide some explanation that could be helpful to other readers too. – cezar Mar 15 '18 at 08:35