I have the following Json
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
What I am trying to do is add a new JToken
Under "message": "Login Required"
something like "RetryMessage": "Failed after 10 retries"
I found this How do you add a JToken to an JObject? which doesn't quite work I think because of the fact that error is a token and not an object but I'm not sure.
What I have tried:
var JsonObj = JObject.Parse(response);
var RetryMessageJson = JToken.Parse(@"{ ""RetryMessage"" : ""UnKnown""}");
JsonObj["error"]["message"].AddAfterSelf(RetryMessageJson);
I have tried several versions of the code above and they all come back with the following Error message:
Newtonsoft.Json.Linq.JProperty cannot have multiple values.
A JObject cannot directly contain a JValue , nor another JObject , for that matter; it can only contain JProperties (which can, in turn, contain other JObjects , JArrays or JValues ).
So you see, a JObject is a JContainer , which is a JToken . Here's the basic rule of thumb: If you know you have an object (denoted by curly braces { and } in JSON), use JObject. If you know you have an array or list (denoted by square brackets [ and ] ), use JArray.
JArray. FromObject(x) takes an object , so it can be used with anything that can be represented as an object and thus certainly an JToken.
Unless the ordering really matters, I suspect you just want to make it another property of the error:
// Variable names edited to follow normal C# conventions
var jsonResponse = JObject.Parse(response);
jsonResponse["error"]["retryMessage"] = "Unknown";
With your sample JSON, that results in:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required",
"retryMessage": "Unknown"
}
}
Although Jon Skeet's answer is 100% correct, in your situation you could accomplish the same thing without explicitly specifying the exact path:
var jsonResponse = JObject.Parse(response);
var newProperty = new JProperty("RetryMessage", "Failed after 10 retries");
jsonResponse.Last.AddAfterSelf(newProperty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With