Teodoro Marinucci
Teodoro Marinucci

Reputation: 37

Linq query to Json string

starting from a JObject I can get the array that interests me:

JArray partial = (JArray)rssAlbumMetadata["tracks"]["items"];

First question: "partial" contains a lot of attributes I'm not interested on. How can I get only what I need?

Second question: once succeeded in the first task I'll get a JArray of duplicated items. How can I get only the unique ones ? The result should be something like

{
'composer': [
                {
                'id': '51523',
                'name': 'Modest Mussorgsky'
                },
                {
                'id': '228918',
                'name': 'Sergey Prokofiev'
                },
        ]
}

Let me start from something like:

[
  {
    "id": 32837732,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Of Thee I Sing: Overture (radio version)"
  },
  {
    "id": 32837735,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Concerto in F : I. Allegro"
  },
  {
    "id": 32837739,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Concerto in F : II. Adagio"
  }
]

Upvotes: 2

Views: 626

Answers (1)

Aliostad
Aliostad

Reputation: 81660

First question:

How can I get only what I need?

There is no magic, you need to read the whole JSON string and then query the object to find what you are looking for. It is not possible to read part of the JSON if that is what you need. You have not provided an example of what the data looks like so not possible to specify how to query.

Second question which I guess is: How to de-duplicate contents of an array of object?

Again, I do not have full view of your objects but this example should be able to show you - using Linq as you requested:

var items = new []{new {id=1, name="ali"}, new {id=2, name="ostad"}, new {id=1, name="ali"}};
var dedup = items.GroupBy(x=> x.id).Select(y => y.First()).ToList();
Console.WriteLine(dedup);

Upvotes: 1

Related Questions