Define classes like this:
class Query
{
[JsonProperty("where")]
public Clause Where { get; set; }
}
class Clause
{
[JsonProperty("operator")]
public string Operator { get; set; }
[JsonProperty("left")]
public Clause Left { get; set; }
[JsonProperty("right")]
public Clause Right { get; set; }
[JsonProperty("$fieldref")]
public string FieldRef { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
Then you can deserialize like this:
Query q = JsonConvert.DeserializeObject<Query>(json);
You can serialize back to JSON like this:
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
json = JsonConvert.SerializeObject(q, settings);
Here is a simple demonstration: https://dotnetfiddle.net/qqAj2v