psyborg.eth
psyborg.eth

Reputation: 320

How to convert JavaScript object containing json string

I am having problem parsing JSON string into JS object .Please tell how to convert JavaScript object :

Object {d: "[{"worker_id":1,"worker_name":"Shivank"}]"}

into

Object { d: [{ "worker_id": 1, "worker_name": "Shivank" }] } 

I have tried using

JSON.parse(data) 

and

var dataFinal = JSON.stringify(data);
var d1 = eval('(' +dataFinal+ ')');

Upvotes: 0

Views: 41

Answers (2)

Jagdish Idhate
Jagdish Idhate

Reputation: 7742

Assuming your data is as below, which d is having stringified json data

var data = {d: "[{\"worker_id\":1,\"worker_name\":\"Shivank\"}]"}

console.log(data);

You can parse JSON and assign to d key

data.d = JSON.parse(data.d)

console.log(data); // required output

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

You have an object where one property value contains JSON so you only need to convert that value

Try

data.d= JSON.parse(data.d);

Upvotes: 2

Related Questions