0

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?

1
  • which library are you using in Scala for handling JSON? Looking at your code, perhaps you are using Play framework. Can you confirm and also checkout this playframework.com/documentation/2.2.x/ScalaJson Commented Sep 27, 2014 at 19:48

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.