1

I have a string that carry the path of a nested json, where I need to update a value

var path = 'p_Data[0]["BusinessUnit"][0]["BusinessUnit"]' 

if i evaluate this path I get value as 'Unit1'. I need to update the value 'Unit1' with 'Closed' and retrieve the original data ie, p_Data

I tried eval(path = 'Closed'), but didn't work.

How to do this?

Okky
  • 10,338
  • 15
  • 75
  • 122
  • did you use jsonpath??? It's a good plugin to manipulate json strings.... – maverickosama92 Aug 07 '13 at 08:44
  • @maverickosama92: isn't there any other way to do this other than having another plugin? – Okky Aug 07 '13 at 08:46
  • FWIW, that's not a JSON string, that's just a string. And I assume you already parsed the JSON data and work with nested objects (so the problem does not have anything to do with JSON). Have a look at [Javascript: How to set object property given its string name](http://stackoverflow.com/q/13719593/218196) for a start. – Felix Kling Aug 07 '13 at 08:53
  • @FelixKling 'JSON string' was a careless mistake sry – Okky Aug 07 '13 at 08:57
  • I haven't worked much with nested JSON this is my first try with nested. – Okky Aug 07 '13 at 08:58
  • Issue is that I have arrays inside these nested json it can be like var path = 'p_Data[0]["BusinessUnit"][1]["BusinessUnit"]' – Okky Aug 07 '13 at 08:59

1 Answers1

1
eval(path + "='Closed'");

But have a look at this thread: Why is using the JavaScript eval function a bad idea?.

Community
  • 1
  • 1
pckill
  • 3,709
  • 36
  • 48