0

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

What if the includeorexclude property has a boolean value? How do I update this?
@user989988 replace (string) by (bool)
I see this error on replacing : ?? can not be applied to operands of type bool and bool
@user989988 I don't have any ??
Sorry to give you more context - I updated the code to includeorexclude = (string)x["includeorexclude"] ?? "include" to provide a default value on updating this to includeorexclude = (bool)x["includeorexclude "] ?? false I get the above error
0

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.

3 Comments

Please show us how this is done in code.
Much better with code. Why are you doing this, though? var result = items.Select(x => x).ToList();
The question was using .Select. I just added useless query line to show example that you can do queries here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.