user3322141
user3322141

Reputation: 158

How to read nested Json using scala?

I am using scala to separate json. I have following Json structure-

"commands":{
"myinfo": [
    {
        "utilization": {
            "sizeBytes": 998848331776,
            "usedBytes": 408722341888,
            "freeBytes": 590125989888
        },
        "name": "ds1",
        "addons": [
            "PQR",
            "ABC"
        ],
        "otherInfo": {
            "model": "MRSASRoMB-4i",
            "name": "naa.6d867d9c7acd60001aed76eb2c70bd53",
            "vendor": "LSI"
        }
    }
]
}}

I want to read value of otherInfo, utilization etc. I can read value of name using following code-

val commandInfo = (rawData \ "Commands").as[JsValue]
(commandInfo \ "myInfo").as[List[JsObject]].map { myJson =>
val name =  (myJson \  "name").asOpt[String]
}

I am using case classes in scala. How do I get values of 'otherInfo','addons' etc using scala?

Upvotes: 0

Views: 208

Answers (1)

Wilfred Springer
Wilfred Springer

Reputation: 10927

Are you using dispatch's JSON support? If so, then you might want to consider using json4s instead, which is little like the de facto standard.

I personally would consider trying to use SON of JSON instead, which would make getting the name out a breeze:

commands.myinfo.otherinfo.name.as[String]

… but then again, that's a shameless plug for something I wrote myself. ;-)

Upvotes: 1

Related Questions