lufekon
lufekon

Reputation: 13

How to count objects in JSON file

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);

Upvotes: 0

Views: 1272

Answers (1)

Peter Csala
Peter Csala

Reputation: 22829

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.

Upvotes: 1

Related Questions