0
votes

I'm using scala play and am attempting to traverse a json tree in order to validate that specific name values have specific children with specific name values. I have the following Json in the form of a JsObject:

{ "name": "user", "children": [ { "name": "$a", "children": [ { "name": "foo", "children": [  ] }, { "name": "fooBar", "children": [ { "name": "$a", "children": [ { "name": "subFoobar1", "children": [  ] }, { "name": "subFoobar2", "children": [ { "name": "TEST", "children": [  ] } ] }, { "name": "subFoobar3", "children": [  ] } ] } ] }, { "name": "bar", "children": [ { "name": "$a", "children": [  ] }, { "name": "$c", "children": [  ] }, { "name": "$b", "children": [  ] } ] }, { "name": "barFoo", "children": [  ] } ] } ] }

Ideally I would use nested for loops to traverse but the JsObject structure is preventing me from accessing the underlying values when attempting traverse. I have also attempted mapping the JsObject to a map of type [Map[String,Map[String,Any]]] but I am getting invalid cast compiler errors.

Any tips on how I can traverse and validate the name value at each level would be appreciated. I would preferably like to use the play json library

1

1 Answers

0
votes

Issue was in the case class I was attempting to use. I wasn't accounting for the recursive nature of my Json structure

case class ActorTree(name : String, children:Seq[ActorTree] )