0

I have a json as below.

{
    "name": "",
    "id": "test1",
    "properties": {
        "name1": [
            {
                "id": "test2",
            },
            {
                "id": "test3"
                }
            }
        ],
        "name3": [
            {
                "id": "test4"


            }
        ],
        "name5": [
            {
                "id": "test7"
            }
        ],
        "name7": [
            {
                "id": "test8"
            }
        ]
    }
}

In the above JSON. I am looking to extract values of all properties which has "id". so finally I need a list that has id values as below.

{"test2", "test3","test4","test7","test8"}

How I can implement this in C#. Thanks for any suggestions?

4
  • What json library are you using? Commented Sep 22, 2021 at 19:25
  • I tried by JsonConvert to convert it into a dynamic type. but I couldn't find how to approach a situation like this? Commented Sep 22, 2021 at 19:28
  • I would also want "test1" in the response that I get. as below. {"test1","test2", "test3","test4","test7","test8"} Commented Sep 22, 2021 at 19:47
  • Please see the update. Commented Sep 23, 2021 at 11:29

2 Answers 2

1

Based on your mention of JsonConvert it seems you are using Json.NET. So one of the options is to use json path to extract needed values:

var js_str= ....;
var jToken = JToken.Parse(js_str);  
var result = jToken.SelectTokens("$.properties.*.[*].id")
    .Values()
    .Select(value => value.Value<string>())
    .ToList();

Or you can query you json with LINQ.

Or create model that represents your data. There is quite common convention for dictionaries to represent object so something like this will work:

public class Root
{
    public Dictionary<string, List<Obj>> Properties { get; set; }
}

public class Obj
{
    public string Id { get; set; }
}

var deserialized = JsonConvert.DeserializeObject<Root>(js_str);
var result = deserialized.Properties
    .Values
    .SelectMany(v => v)
    .Select(obj => obj.Id)
    .ToList();

UPD

To get values of all properties with name == id you can do this:

var res = JObject.Parse(js_str)
    .Descendants()
    .OfType<JProperty>()
    .Where(p => p.Name == "id")
    .Select(p => p.Value.ToString())
    .ToList();

Or update Root model to have Id property.

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

Comments

0

Using JToken.Parse does not need to create the classes, but as I guess you may need not only Ids but some another data too. In this case try this code that is using JsonConvert

var json="{\"name\":\"\",\"id\":\"test1\",\"properties\":{\"name1\":[{\"id\":\"test2\"},{\"id\":\"test3\"}],\"name3\":[{\"id\":\"test4\"}],\"name5\":[{\"id\":\"test7\"}],\"name7\":[{\"id\":\"test8\"}]}}";

var jD=JsonConvert.DeserializeObject<Root>(json);
    
var ids=jD.properties.Select(p =>p.Value.Select(v =>v.id ) ).SelectMany(i=> i).ToList();

output

["test2", "test3","test4","test7","test8"]

classes

public class Root
{
    public string name { get; set; }
    public string id { get; set; }
    public Dictionary<string,Name[]> properties { get; set; }
}
public class Name
{
    public string id { get; set; }
}

Comments

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.