-2

I have a object convert into json string using JSON.stringify() and then I need same object with using of JSON.parse(), but I get array values are empty, how to I get original data, please help to solve this issue, in below codes is stringify data

{
"agefrom":18,
"ageto":60,
"heightfrom":"1",
"heightto":"28",
"steps":["Never","One"],
"number":["One","two "],
"education":["B.E","B.E / B.Tech"]
}

below data after convert into JSON.parse

 {
        "agefrom":18,
        "ageto":60,
        "heightfrom":"1",
        "heightto":"28",
        "steps":"",
        "number":"",
        "education":""
   }
  • 2
    If your first object is your actual object (`obj`) doing `console.log(JSON.parse(JSON.stringify(obj)));` gives back the same object, with the array values - no data is lost from stingifying and then parsing it – Nick Parsons Mar 10 '19 at 12:58
  • console.log return correct value, but here I assign that value like let val = JSON.parse(string_data); console.log(val); empty data like second object – Anbuganesh Kannan Mar 10 '19 at 13:21

1 Answers1

2

fiddle try this fiddle. it works fine for me.

 a = {
  "agefrom": 18,
  "ageto": 60,
  "heightfrom": "1",
  "heightto": "28",
  "steps": ["Never", "One"],
  "number": ["One", "two "],
  "education": ["B.E", "B.E / B.Tech"]
}
a = JSON.stringify(a)
document.write(a);
b = JSON.parse(a);
document.write(JSON.stringify(b))