J.SMTBCJ15
J.SMTBCJ15

Reputation: 509

I want to add value to existing Json without double quotes

Initially I have Json like this

{
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

I want to add "status": {success: true} before “softwareName” for which I have done this

JObject rss = JObject.Parse(jsonString);
rss.Property("softwareName").AddBeforeSelf(new JProperty("status", "{success: true}"));

which give me results like this

{
  "status": "{success: true}",
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

But, I want to get result like this;

{
  "status": {success: true},
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

i.e status value without double quotes. How can I achieve this desired result? Because after that i'll have this view as show below

enter image description here

but currently I have it like this

enter image description here

Upvotes: 4

Views: 1530

Answers (2)

Chibueze Opata
Chibueze Opata

Reputation: 10034

You want to do:

JObject rss = JObject.Parse(jsonString);
rss.Property("softwareName").AddBeforeSelf(new JProperty("status", JObject.Parse("{success: true}")));

Basically, you need to parse the string as a JSON object before adding it as property.

Upvotes: 3

Ankit Raonka
Ankit Raonka

Reputation: 6857

try this

obj  = {
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

var newKey = "status"
var newVal = {success : true}

obj[newKey] = newVal

it gives

obj  = {
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
  "status": {success : true}
}

Upvotes: 2

Related Questions