0

I'm trying to read a .json file and in my C# project and count the amount of objects inside an other object.

The .json file is fix and I'm not able to change anything from it.

e.g.

The .json file looks like this:

{
  "X": {
    "x": "...",
    "x1": "..."
  },
  "Y": {
    "y": { }
    "y1": { },
    "y2": { },
    "y3": { },
    "y4": { },
    "functiongroup": {
        "function1": {
            "A": "...",
            "B": "...",
            "C": "...",
            "D": "...",
            "subfunction": {
                "sub1": {
                    "a": "...",
                    "b": "...",
                    "c": "..."
                }
            }
        },
        "function2": {
            "A": "...",
            "B": "...",
            "C": "...",
            "D": "...",
            "subfunction": {
                "sub1": {
                    "a": "...",
                    "b": "...",
                    "c": "...",
                    "d": "..."
                },
                "sub2": {
                    "a": "...",
                    "b": "...",
                    "d": "..."
                },
                "sub3": {
                    "a": "...",
                    "b": "...",
                    "c": "..."
                }
            }
        },
        "function3": {
            "A": "...",
            "B": "...",
            "C": "...",
            "D": "...",
            "subfunction": {
                "sub1": {
                    "a": "...",
                    "b": "...",
                    "c": "...",
                    "d": "..."
                },
                "sub2": {
                    "a": "...",
                    "b": "...",
                    "d": "..."
                },
                "sub3": {
                    "a": "...",
                    "b": "...",
                    "c": "..."
                }
            }
        },
       "function2": {
            "A": "...",
            "B": "...",
            "C": "...",
            "D": "...",
            "subfunction": {
                "sub1": {
                    "a": "...",
                    "b": "...",
                    "c": "...",
                    "d": "..."
                },
                "sub2": {
                    "a": "...",
                    "b": "...",
                    "d": "..."
                },
                "sub3": {
                    "a": "...",
                    "b": "...",
                    "c": "...""
                },
                 sub4": {
                    "a": "...",
                    "b": "...",
                    "c": "..."
                }
            }
        }
    }
 
}

Now i want to know how many subfunctions there are in every function and print the amount in the Console. So in the shown example it would look like this:

OUTPUT

1, 3, 3, 4

I´ve tried multiple variants like

var JsonFile = System.IO.File.ReadAllText(@"PathtoFile");

var token = JToken.Parse(JsonFile);
var FG = token.Value<JArray>("subfunction");
int count = FG.Count();

Console.WriteLine(count);
5
  • Based on the type names in the question, it looks like you're using Json.NET. This library also has its own tag that you can add if this is the case. Since the using directives aren't in your question, I can't be sure, so I won't add the tag myself. Commented Oct 7, 2021 at 6:16
  • HM it looks like you have properties instead of array items (in JSON array is denoted by [ ]) Commented Oct 7, 2021 at 6:22
  • 1
    Your file is not valid JSON. It must start either with { or [ and it is missing some closing tags. Can you please provide a valid file? Commented Oct 7, 2021 at 6:51
  • what about parse json file as string and try to find amount of "subfunction" strings? yes, it may be tricky... but :) Commented Oct 7, 2021 at 7:41
  • @lufefon Could you please amend your question to post a valid json. This website can help you to identify the problems with your current json. Commented Oct 8, 2021 at 6:57

1 Answer 1

1

After fixing your sample json:

{
  "X": {
    "x": "...",
    "x1": "..."
  },
  "Y": {
    "y": {

    },
    "y1": {

    },
    "y2": {

    },
    "y3": {

    },
    "y4": {

    },
    "functiongroup": {
      "function1": {
        "A": "...",
        "B": "...",
        "C": "...",
        "D": "...",
        "subfunction": {
          "sub1": {
            "a": "...",
            "b": "...",
            "c": "..."
          }
        }
      },
      "function2": {
        "A": "...",
        "B": "...",
        "C": "...",
        "D": "...",
        "subfunction": {
          "sub1": {
            "a": "...",
            "b": "...",
            "c": "...",
            "d": "..."
          },
          "sub2": {
            "a": "...",
            "b": "...",
            "d": "..."
          },
          "sub3": {
            "a": "...",
            "b": "...",
            "c": "..."
          }
        }
      },
      "function3": {
        "A": "...",
        "B": "...",
        "C": "...",
        "D": "...",
        "subfunction": {
          "sub1": {
            "a": "...",
            "b": "...",
            "c": "...",
            "d": "..."
          },
          "sub2": {
            "a": "...",
            "b": "...",
            "d": "..."
          },
          "sub3": {
            "a": "...",
            "b": "...",
            "c": "..."
          }
        }
      },
      "function4": {
        "A": "...",
        "B": "...",
        "C": "...",
        "D": "...",
        "subfunction": {
          "sub1": {
            "a": "...",
            "b": "...",
            "c": "...",
            "d": "..."
          },
          "sub2": {
            "a": "...",
            "b": "...",
            "d": "..."
          },
          "sub3": {
            "a": "...",
            "b": "...",
            "c": "..."
          },
          "sub4": {
            "a": "...",
            "b": "...",
            "c": "..."
          }
        }
      }
    }
  }
}

you can retrieve the desired values like this:

JObject semiParsedJson = JObject.Parse(json);
var functionGroup = semiParsedJson["Y"]["functiongroup"];

for (int i = 1; i < 5; i++)
{
    var function = functionGroup[$"function{i}"];
    var subFunctions = function["subfunction"];
    Console.WriteLine(subFunctions.Count());
}

Please bear in mind that the indexer operator is fragile. Please prefer TryGetValue in production code to perform existence check before moving down the path.

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

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.