GroomedGorilla
GroomedGorilla

Reputation: 1002

Extracting sub-objects (as JSON) from a JSON file

I'm working on a JSON file with nested objects and would like to extract the child objects without converting them to their Scala case class equivalents. Is there any pre-built functionality to filter out chunks of JSON text this way?

For example, if I've got a JSON file with content similar to this:

{
  "parentObject": "bob",
  "parentDetail1": "foo",
  "subObjects": [
    {
      "childObjectName": "childname1",
      "detail1": "randominfo1",
      "detail2": "randominfo1"
    },
    {
      "childObjectName": "childname2",
      "detail1": "randominfo2",
      "detail2": "randominfo2"
    },
    {
      "childObjectName": "childname3",
      "detail1": "randominfo3",
      "detail2": "randominfo3"
    }
  ]
}

I would like to extract the subObjects nodes, ideally as individual chunks of JSON text (perhaps as an String Array with each subObject as an element). I know I could parse the entire JSON file into objects I've pre-defined in Scala classes, but would rather not take that route since this will probably be too expensive for larger files. I'm looking for a simple and elegant way to go here. Any ideas?

Upvotes: 2

Views: 1565

Answers (2)

Richard Bowker
Richard Bowker

Reputation: 109

solution using json-lenses and spray json

import spray.json.DefaultJsonProtocol._
import spray.json._
import spray.json.lenses.JsonLenses._

object Main extends App {

 val jsonData =
   """
     |{
     |  "parentObject": "bob",
     |  "parentDetail1": "foo",
     |  "subObjects": [
     |    {
     |      "childObjectName": "childname1",
     |      "detail1": "randominfo1",
     |      "detail2": "randominfo1"
     |    },
     |    {
     |      "childObjectName": "childname2",
     |      "detail1": "randominfo2",
     |      "detail2": "randominfo2"
     |    },
     |    {
     |      "childObjectName": "childname3",
     |      "detail1": "randominfo3",
     |      "detail2": "randominfo3"
     |    }
     |  ]
     |}
   """.stripMargin.parseJson


  val subObjectsLens = 'subObjects / *

  val subObjects = jsonData.extract[JsValue](subObjectsLens)

  println(subObjects map {_.compactPrint} mkString ", ")
}

Upvotes: 2

mohit
mohit

Reputation: 4999

Most of the JSON libraries provide some kind of feature to extract nested JSON. You haven't mentioned properly how you want the output(String Array with each subObject as an element ?? Do you want the fields of subObject to be merged into a single string??), I will leave the answer to extraction of nested JSON.

JSON4s

val json = parse(""" {
                    "parentObject": "bob",.... }""")
val subObjects = (json \"subObjects") 
// Returns a JArray(internal representation of JSON Array in Json4s). It has a flexible DSL
//which you can use to extract the fields as you like. 

Play-Json

val json = Json.parse("""{ "parentObject": "bob",.... }""")
val subObjects = (json \"subObjects")
//>subObjects  : play.api.libs.json.JsValue =  [{"childObjectName":"childname1", "detail1":"randominfo1", ....

Other libraries too should have similar features.

Upvotes: 0

Related Questions