How to deserialise specific keys of json object?

newtonsoft json deserialize
deserialize json to object c#
jtoken to jobject
jobject get value
c# json to object
newtonsoft json deserialize partial
c# json deserialize child object
javascript deserialize json to class

Input JSON file:

{
    "@version": "2.7.0",
    "@generated": "Wed, 30 May 2018 17:23:14",
    "site": {
        "@name": "http://google.com",
        "@host": "google.com",
        "@port": "80",
        "@ssl": "false",
        "alerts": [
            {

                "alert": "X-Content-Type-Options Header Missing",
                "name": "X-Content-Type-Options Header Missing",
                "riskcode": "1",
                "confidence": "2",
                "riskdesc": "Low (Medium)",
                "desc": "<p>The Anti-MIME-Sniffing header X-Content-Type-Options was not set to 'nosniff'. This allows older versions of Internet Explorer and Chrome to perform MIME-sniffing on the response body, potentially causing the response body to be interpreted and displayed as a content type other than the declared content type. Current (early 2014) and legacy versions of Firefox will use the declared content type (if one is set), rather than performing MIME-sniffing.</p>",
                "instances": [
                    {
                        "uri": "http://google.com",
                        "method": "GET",
                        "param": "X-Content-Type-Options"
                    }
                ],          
                "wascid": "15",
                "sourceid": "3"
            }

        ]
    }
}

Expected Output: List alerts;

where:

public class Alert
{
    public string alert;
    public string riskcode;
}

I want to fetch particular keys of the json object ahe deserialise it in the alert object.

The simplest way is to just declare the outer objects with enough keys to reach the keys you care about:

public class Alert
{
    public string alert;
    public string riskcode;
}

public class SiteAlerts
{
    public Site site { get; set; }
}

public class Site
{
    public List<Alert> alerts { get; } = new List<Alert>();
}

Then you can simply deserialize with:

var siteAlerts = JsonConvert.DeserializeObject<SiteAlerts>(json);
var alerts = siteAlerts.site.alerts; // no error-checking here

Deserializing Partial JSON Fragments, a small fragment of information. This scenario can be annoying when you want to deserialize that JSON fragment into .NET objects because you have to define . The camel case naming policy for dictionary keys applies to serialization only. If you deserialize a dictionary, the keys will match the JSON file even if you specify JsonNamingPolicy.CamelCase for the DictionaryKeyPolicy. Enums as strings. By default, enums are serialized as numbers. To serialize enum names as strings, use the JsonStringEnumConverter.

I recommend you to use Newtonsoft.Json library to make it easy for deserializing json data.

If you want a partial deserialization e.g. only deserializing the alerts property into your class Alert without creating the whole strcuture of classes required.

You can use this code:

JObject jObject = JObject.Parse(json);
var alerts = jObject["site"]["alerts"].ToObject<Alert[]>();
foreach(var item in alerts)
{
    Console.WriteLine("alert: " + item.alert);  
    Console.WriteLine("riskcode: " + item.riskcode);
}

Complete demo available here.

Using JObject.Properties, Json.Linq.JProperty collection using M:Newtonsoft.Json.Linq.JObject.Properties. Deserializing from JSON with LINQ · Serializing This sample gets an object's JProperty collection using Properties(). Sample Key + " - " + property.Value);  Step 1: Deserialize the base JSON string. Since the string is surrounded by curly braces {}, it represents a JSON object. So, we have to use Deserialize JSON activity on the string. After using the deserialize activity, we will get the values of the following parameters. Let us assume we saved the output of activity into a variable named personData.

Short version

var siteAlerts = JsonConvert.DeserializeObject<dynamic>(json).site.alerts.ToObject<Alert[]>();

Deserializing a JSON into a JavaScript object, How to move mouse pointer to a specific position using JavaScript ? How to Sort/​Order keys in JavaScript objects ? How to store a key=> value array in JavaScript ? To Deserialize a JSON into a JavaScript object, here we will use a common  This sample deserializes JSON to an object. Json.NET Documentation. Json.NET Documentation. Samples. Serializing JSON. Serialize an Object. Deserialize an Object

Deserializing duplicate JSON object keys to a dictionary · Issue , In Newtonsoft.Json key/value pairs are set into the dictionary so that the last value in an object wins. This means dictionaries align with classes  Deserialize inferred types to object properties; Support Dictionary with non-string key; Support polymorphic deserialization; Deserialize inferred types to object properties. When deserializing to a property of type object, a JsonElement object is created. The reason is that the deserializer doesn't know what CLR type to create, and it doesn't try to guess.

How to serialize and deserialize JSON using C#, Json namespace to serialize and deserialize to and from JavaScript Object Notation scenarios and customization specific to serialization and deserialization. By default, property names and dictionary keys are unchanged in the JSON  And the JSON I got is of a multi level nested JSON. I searched a lot in Google on how to hold that JSON data into a custom C# List of objects, but found no help. After a long research and trying my hands here and there I found the best solution.

How do you deserialize json properties that are reserved words in , I've created a abstract class to allow for two-way serialization of JSON that has reserved typeMapKeys = typeMapKeys; } public String serialize(Object obj, System. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT​. Newtonsoft Json Deserialize Dictionary as Key/Value list from DataContractJsonSerializer Tag: c# , json , dictionary , json.net , deserialization I have a dictionary serialized to storage with DataContractJsonSerializer which I would like to deserialize with Newtonsoft.Json.

Comments
  • What have you tried already?
  • You want to skip down to the "alerts": [...] part and avoid the outer object? You should basically create empty wrapper types with just the keys necessary to reach that key.
  • Are uou using json.net library?