2

I have this JSON string value as defined below. I want to get he value for "code" only. I tried the solution in the internet but I am only getting a null value. Any idea on this?

 JObject json = JObject.Parse(JSON string here);
 var jsonData = json.SelectToken("serviceMessage.code");    <-- THIS RETURNS NULL

The json:

{
   "highestSeverity":"Error",
   "serviceMessage":[
      {
         "code":"3004",
         "severity":"Error",
         "reason":"Reason here."
      }
   ]
}
2
  • newtonsoft.com/json/help/html/deserializeobject.htm Commented Sep 21, 2021 at 13:35
  • 5
    Hint: serviceMessage is an array . Therefore it doesn't have a "code" property, but the first item within the array does... Commented Sep 21, 2021 at 13:35

3 Answers 3

4

Basically you can parse it to a model and get the value for code. E.g:

public class ServiceMessage
{
     public string code { get; set; }
     public string severity { get; set; }
     public string reason { get; set; }
}

public class Root{
   public string highestSeverity { get; set; }
   public List<ServiceMessage> serviceMessage { get; set; }
}

public class AnotherClass
{
   public string MyFunction(string myJson){
       var data = JsonConvert.DeserializeObject<Root>(myJson);
       //Then you can access the value in the object
       return data.serviceMessage.FirstOrDefault().code;
   }
}
Sign up to request clarification or add additional context in comments.

7 Comments

No, because serviceMessage isn't an object, and also it isn't the outer item in the JSON
@ADyson "serviceMessage isn't an object, and also it isn't the outer item in the JSON" is a completely wrong statement. "serviceMessage" is indeed a list of objects in a JSON notation (link see reference), and is indeed an outer "item" since it's not enclosed by another object.
Sorry but no. "serviceMessage" is indeed a list of objects...yes. It's a list. It's not an object. In JSON, objects are surround by { }, lists are surrounded by [ ]. and the way you parse them and access the items within them is different. And it isn't the outer item because it's a named property inside the enclosing outer object. Even your hasty edit to add a "list" to the deserialisation doesn't help because you're still targeting only part of the object, and your method now tries to access the "code" property of a list.
FYI here's how it should actually work using a strongly-typed deserialisation approach: dotnetfiddle.net/IgVFcl
You can't ignore the rest of the JSON, it has to be parsed as a single item. Upvoted now you've edited to a usable version
|
3

As serviceMessage is an array, assuming you want the first item you can simply use

var code = json["serviceMessage"][0]["code"];

Live example: https://dotnetfiddle.net/2WgkcF

Comments

2

As it was indicated by ADyson your serviceMessage in an array not an object. So, you have to treat it as a collection.

  1. retrieve the collection
  2. retrieve the first element from it
  3. finally, you can get the code value
JObject json = JObject.Parse(rawJson);
JArray messages = (JArray)json["serviceMessage"];
string code = messages.FirstOrDefault()?["code"]?.Value<string>()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.