get JSON from hierarchical object in JCR API

111 Views Asked by At

I understand how the JCR API works and is used in Magnolia. I want to get the result as JSON object My Node object has a hierarchical structure(each subnode has type mgnl:category)

test_1
  test_a
  test_b
  test_c
     test_c1    
  test_d

If I use

  var session = context.getJCRSession("category");
  Iterable<Node> categoryItems = NodeUtil.collectAllChildren(
      session.getNode(nodePath),
      new NodeTypePredicate("mgnl:category")); 
  List<String> result = new ArrayList<>();
  for (Node node : categoryItems) {
    result.add(node.getName());
  }

I get just a list of children like: [test_a, test_b, test_c, text_c1, test_d]. How can I check if a child has a subnode? Because I need [test_a, test_b, test_c: {text_c1}, test_d]. I think recursion will do here... but I need info about if a node has a subnode...

1

There are 1 best solutions below

0
knguyen On

You can check if a node has a subnode by hasNodes() method. You can refer more JCR Node APIs here https://developer.adobe.com/experience-manager/reference-materials/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Node.html Thanks