Hexxed
Hexxed

Reputation: 693

How to query into a JSON getting using LINQ?

JSON

{
  "count": 3,
  "value": [
    {
      "id": "AAAAAAAAAAAAA",
      "description": "test1",
      "name": "name1"
    },
    {
      "id": "BBBBBBBBBB",
      "description": "test2",
      "name": "name2"
    },
    {
      "id": "CCCCCCCCCCCC",
      "description": "test3",
      "name": "name3"
    }
  ]
}

I have a code in my solution retrieving from a LIST api and giving the JSON above. How can I use a LINQ to retrieve specific values? (e.g) I need to select name1 and I will get the id,description,name values.

I am using a dynamic variable in my code:

dynamic json = JObject.Parse(client.GetString().Result);

I'd been tinkering with other online guides the past few hours. However, can't get the result right.

Please help.

Upvotes: 1

Views: 4139

Answers (4)

Hexxed
Hexxed

Reputation: 693

Apparently with fiddling with my code I found an answer for myself. Thanks for to the ones trying to help me for giving me ideas.

var requestWorkProcess = await client.GetStringAsync("my url");
var workProcessId = JObject.Parse(requestWorkProcess)["value"].Children<JObject>().FirstOrDefault(o => o["name"].ToString() == workProcess).GetValue("id");

Upvotes: -1

Kirk Larkin
Kirk Larkin

Reputation: 93293

First, you can create a class to represent a client:

public class Client
{
    public string Id { get; set; }
    public string Description { get; set; }
    public string Name { get; set; }
}

With this class, you can use JObject.Parse (as you're already doing) to parse the JSON into something that can be queried, use SelectToken to pull out the value array and then use ToObject to convert that to a list of Clients. Here's what that looks like:

var jsonObject = JObject.Parse(json_source);
var jsonObjectValue = jsonObject.SelectToken("value");
var clients = jsonObjectValue.ToObject<List<Client>>();

Once you've got your clients variable, you can use a simple LINQ statement to find the one that is name1:

var clientWithName1 = clients.SingleOrDefault(x => x.Name == "name1");

In this case, clientWithName will be null if no such client was found.

Here's a dotnetfiddle that demonstrates a complete solution.

Upvotes: 2

Rui Jarimba
Rui Jarimba

Reputation: 18169

One solution would be to deserialize your JSON string into C# objects and then use Linq to get a specific object.

C# class definitions:

public class Content
{
    [JsonProperty("count")]
    public int Count { get; set; }

    [JsonProperty("value")]
    public List<Value> Values { get; set; }

    public Content()
    {
        Values = new List<Value>();
    }
}

public class Value
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

Deserializing and getting the object:

string json = @"{
""count"": 3,
""value"": [
  {
    ""id"": ""AAAAAAAAAAAAA"",
    ""description"": ""test1"",
    ""name"": ""name1""
  },
  {
    ""id"": ""BBBBBBBBBB"",
    ""description"": ""test2"",
    ""name"": ""name2""
  },
  {
    ""id"": ""CCCCCCCCCCCC"",
    ""description"": ""test3"",
    ""name"": ""name3""
  }
]
}";


Content content = JsonConvert.DeserializeObject<Content>(json);

Value value = content.Values.FirstOrDefault(x => x.Name.Equals("name1", StringComparison.InvariantCultureIgnoreCase));

Upvotes: 4

Peter Marshall
Peter Marshall

Reputation: 1355

Create an object Client that has properties id, description and name. Deserialize the json into a list of these objects.

List<Client> clients = JsonConvert.Deserialize<List<Client>>(json_source);
string desc = clients[0].description;

Upvotes: 1

Related Questions