Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to identify if the json is nested or not. If it contains a second object after the first then it will return true if not false.

function check_if_nested(obj) {
    check_nest=[]
    obj.map(function(e,i) {$.each(e, function(v){
      if(typeof(obj[0][v])=='object') {check_nest.push('nested')} else {check_nest.push('not nested')}
    })})
    if(check_nest.includes('nested')) {return(true)} else {return(false)}
    }


//Not nested
obj_1 = [{
    one: "apples",
    two: "oranges"
}]



Usage: check_if_nested(obj_1)

output: false

obj_2 = [{
    one: "apples",
    two: "oranges",
    children: [{
        three: "bananas",
        four: "jicamas"
    }]
}]


Usage: check_if_nested(obj_2)

output: true

What I have tried:

new to c# I am not sure how to do this..
Posted
Updated 17-Jul-20 0:56am
Comments
Dave Kreskowiak 16-Jul-20 20:36pm    
What you posted is not C#. It's javascript.
Member 14779968 16-Jul-20 20:39pm    
can we write is in C#?

What your'e calling "nested" is how json describes an array.

You cannot directly convert that javascript to C#. C# doesn't handle json natively like javascript does.

You have to inspect the json using a class dedicated for the purpose, like JsonDocument[^].

You'd start by looking at the type of the root node in the document. Is it an array or not? Move on to the next element, looking for whatever you're expecting based on what you've found already.
 
Share this answer
 
You can do this with the Newtonsoft.Json package, no doubt with any other json handling library too.

C#
private void ParseJson (JToken token)
{
    foreach (var prop in token.Children())
    {
        if (prop.Type == JTokenType.Property)
        {
            var p = prop as JProperty;

            bool isNested = IsNested(p);

            Console.WriteLine(p.Name + " " + isNested.ToString());
        }

    }
}

private bool IsNested(JProperty token)
{
    bool isNested = false;

    if (token.Value.Type == JTokenType.Object)
    {
        isNested = true;
    }
    else if (token.Value.Type == JTokenType.Array)
    {
        isNested = true;
    }

    return isNested;
}


usage;

C#
var tokens = new List<JToken>();

string json = "[{one:\"apples\", two:\"oranges\", children:[{three:\"bananas\"}]}]";

dynamic jResult = JsonConvert.DeserializeObject(json);

var token = jResult as JToken;

if (token.Type == JTokenType.Array)
{
    tokens.AddRange(token.Children());
}
else
{
    tokens.Add(token);
}

foreach (var item in tokens)
{
    ParseJson(item);
}


That's a starting point anyway, it will probably need more work to make it more robust and to handle a greater variety of json, it depends how variable your json in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900