1

I'm new to json. I'm trying to filter json data using linq to json query in C#. I'm trying to retrieve multiple values from json data:

string json= @"{
 "parts": [
    {

      "attributes": {
        "Motherboard": "Gigabyte GA-H81M-S2H",
        "Max RAM": "16GB",
        "Form Factor": "Micro ATX",
        "RAM Slots": 2,
        "Socket / CPU": "LGA1150"
      },
      "type": "Motherboard",
      "name": "Gigabyte GA-H81M-S2H",
      "slug": "gigabyte-motherboard-gah81ms2h"
    },
    {
        "attributes": {
        "Motherboard": "MSI H55-G43",
        "Max RAM": "16GB",
        "Form Factor": "ATX",
        "RAM Slots": 4,
        "Socket / CPU": "LGA1156"
      },
      "type": "Motherboard",
      "name": "MSI H55-G43",
      "slug": "msi-motherboard-h55g43"
    },
    {
      "url": "http://pcpartpicker.com/part/asus-motherboard-rampageivblackedition",
      "attributes": {
        "Motherboard": "Asus Rampage IV Black Edition",
        "Max RAM": "128GB",
        "Form Factor": "EATX",
        "RAM Slots": 8,
        "Socket / CPU": "LGA2011"
      },
      "type": "Motherboard",
      "name": "Asus Rampage IV Black Edition",
      "slug": "asus-motherboard-rampageivblackedition"
    }
 ],

}";

This is my C# code:

JObject results = JObject.Parse(json);
  var categories = from c in results["parts"]["attributes"].Children()["Motherboard"].Values<string>()
                         group c by c
                         into g
                         orderby g.Count() descending
                         select new { Category = g.Key, Count = g.Count() };

I tried with this code and it not returns any result.Please let me know where I'm doing mistake or is this the proper way to write the query.Can anyone please help me to solve this.

dbc
  • 104,963
  • 20
  • 228
  • 340
Rajesh
  • 55
  • 2
  • 11

1 Answers1

1

You can use SelectTokens for queries of this nature. It supports JSONPath query syntax. Thus:

        var categories = from c in results.SelectTokens("parts[*].attributes.Motherboard")
                         group c by c
                             into g
                             orderby g.Count() descending
                             select new { Category = (string)g.Key, Count = g.Count() };

In your JSON, the value of "parts" : [...] is an array; in the query, "parts[*]" is a wildcard that searches through all elements of the array.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Thanks for the reply.It's working,how to select multiple objects – Rajesh Aug 25 '15 at 09:33
  • @Rajesh - Your comment doesn't have enough detail for me to be sure how to answer, so you might want to ask another question. The preferred format on stackoverflow is [one question per question](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts). – dbc Aug 25 '15 at 09:47
  • sorry,If i want to select name,type from my json data how to pass in the select token – Rajesh Aug 25 '15 at 10:11
  • one more help can you provide links for linq to json tutorial – Rajesh Aug 25 '15 at 10:14