Home > Mobile >  Filter JSON Array based on element value in C#
Filter JSON Array based on element value in C#

Time:12-15

I have this JSON by using: var root = JToken.Parse(myInputJson);

{{
  "results": [
    {
      "orderId": "A123456789",
      "genId": 3,
      "invoiceId": "001",
      "locId": "D07",
      "group": "",
      "assignedUserId": "TEST",
      "billTo": 66537,
      "shipTo": 66537,
      "shipToName": "CONSTRUCTION TEST",
      "pickCount": "1",
      "shipBy": "UPS",
      "isFromMultipleZones": false,
      "taskState": "Assigned",
      "boxes": []
    },
    {
      "orderId": "B987654321",
      "genId": 3,
      "invoiceId": "001",
      "locId": "D08",
      "group": "",
      "assignedUserId": "",
      "billTo": 66537,
      "shipTo": 66537,
      "shipToName": "CONSTRUCTION TEST",
      "pickCount": "4",
      "shipBy": "UPS",
      "isFromMultipleZones": false,
      "taskState": "Assigned",
      "boxes": []
    }
  ]
}

What I need to is to get all the elements between the braces where the orderId = "B987654321".

After researching I have got this far but is not producing what I need:

var root = JToken.Parse(myInputJson);
var values = root.Where(t => (string)t["orderId"] == "B987654321").ToList();

I think I am close, can anyone see where my mistake is?

CodePudding user response:

You forgot to index root by "results":

var root = JToken.Parse(text);
var values = root["results"].Where(t =>(string)t["orderId"] == "B987654321");

Also, your example JSON is badly formatted. If you delete the very first opening brace ({) it works

CodePudding user response:

To get an order as JToken you can use this code

var results = JObject.Parse(json).SelectToken("results").ToArray();

var jsonOrder =  results.First(o => o.SelectToken("orderId").ToString() == "B987654321");

but since you are using c# , it is much easier and safer to use an instance of Order class instead of JToken

Order order=jsonOrder.ToObject<Order>();

Order class

    public partial class Order
    {
        [JsonProperty("orderId")]
        public string OrderId { get; set; }

        [JsonProperty("genId")]
        public long GenId { get; set; }

        [JsonProperty("invoiceId")]
        public string InvoiceId { get; set; }

        [JsonProperty("locId")]
        public string LocId { get; set; }

        [JsonProperty("group")]
        public string Group { get; set; }

        [JsonProperty("assignedUserId")]
        public string AssignedUserId { get; set; }

        [JsonProperty("billTo")]
        public long BillTo { get; set; }

        [JsonProperty("shipTo")]
        public long ShipTo { get; set; }

        [JsonProperty("shipToName")]
        public string ShipToName { get; set; }

        [JsonProperty("pickCount")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long PickCount { get; set; }

        [JsonProperty("shipBy")]
        public string ShipBy { get; set; }

        [JsonProperty("isFromMultipleZones")]
        public bool IsFromMultipleZones { get; set; }

        [JsonProperty("taskState")]
        public string TaskState { get; set; }

        [JsonProperty("boxes")]
        public List<object> Boxes { get; set; }
    }

But it is much easier to deseialize the whole json as net object instead of parsing. You can use linq to get any data.

List<Order> orders  =JsonConvert.DeserializeObject<Root>(json).Orders;
Order order =  orders.First(o => o.OrderId == "B987654321");

Root class

public partial class Root
{
    [JsonProperty("results")]
    public List<Order> Orders { get; set; }
}
  • Related