user989988
user989988

Reputation: 3736

How to query multiple fields from json string?

I have the following json string:

[
    {
        "id": 1,
        "type": "bird",
        "includeorexclude": "include"        
    },
    {
        "id": 2,
        "type": "animal",
        "includeorexclude": "include"       
    },
    {
        "id": 3,
        "type": "animal",
        "includeorexclude": "exclude"       
    }
]

And here is the code to select type from each json object:

var queries = JArray.Parse(json);
var queryTypes = queries.SelectTokens("$..type")
                        .Select(x => x.Value<string>())
                        .ToList();


foreach (var type in queryTypes)
{
    // var message = CreateMessage();
    // message.UserProperties.Add("Type", type);    
}

How do I get both type and includeorexclude from each json object?

Upvotes: 0

Views: 1159

Answers (2)

Emopusta
Emopusta

Reputation: 90

You can just create a class that has properties type and includeorexclude. Then you create new objects with that class and assign your data with your query. After that a generic list can contain all you need.


I think words are not enough for the solution here my perspective about this question.

This is my main class that does the job:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace stackoverflow
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader r = new StreamReader("stack1.json"))
            {
                // some convertion and reading from json file
                string json = r.ReadToEnd();
                List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);

                // do your query here with linq
                var result = items.Select(x => x).ToList();

                //writing result
                foreach (var item in result)
                {
                    Console.WriteLine(item.type);
                    Console.WriteLine(item.includeorexclude);
                   
                }
            }
            
        }
    }
}

and this is the class that i told you add properties that you needed.

// Item that includes what is in json file
    internal class Item
    {
        public int id { get; set; }
        public string type { get; set; }
        public string includeorexclude { get; set; }
    }

I hope this helps you to understand what i meant.

Upvotes: 0

Serge
Serge

Reputation: 43880

you can use an anonymos type

    var jsonParsed = JArray.Parse(json);

    var queryTypes = jsonParsed.Select(x => new
    {
        type = (string)x["type"],
        includeorexclude = (string)x["includeorexclude"]
    }
    ).ToList();

    foreach (var type in queryTypes)
    {
        Console.WriteLine(type.type);
        Console.WriteLine(type.includeorexclude);
    }

or create a class instead of an anonymous type

Upvotes: 2

Related Questions