Parse Json in Scala

In this post, I am going to review how to parse json format data in Scala.

Environment OS: Ubuntu 16.04 Java: openjdk version "1.8.0_131" Scala: 2.10 Build Tool: Apache Maven 3.5.0 IDE: IntelliJ

Add dependencies to pom.xml To parse json format string in Scala, play-json moduleinPlayFramework is available. So add the following dependencies to your pom.xml file.

<!-- https://mvnrepository.com/artifact/com.typesafe.play/play-json_2.10 -->
<dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-json_2.10</artifactId>
    <version>2.6.2</version>
</dependency>

Brief code review Overview of workflow To test parsing json format data in Scala, the below workflow is followed. 1. Parse json format data and retrieve information, such as IP and user/password, to access XtremIO storage. 2. Send a HTTP GET request and recieve a response in Json-like formatted string data. 3. Convert the string response into json and parse the json data in the responce.

Note that, XtremIO storage used in this test is actually a virtual XMS (XtremIO Management Server), which can be used to simulate how XIO API response works.

  1. Read json file First of all, a json file which contains XMS information is prepared before the test. The json file has the information like below.
{
        "ip": "192.168.242.128",
        "user": "admin",
        "password": "Xtrem10",
        "clusters": [
		{
			"cluster": {
				"name": "xbrick113",
				"logical-space-in-use": "true",
				"ud-ssd-space-in-use": "true",
				"data-reduction-ratio": "true",
			}
        ***** omitted *****    
		}
    ]
}

To pass the json data to the program as input, java.io.FileInputStream is used. https://stackoverflow.com/questions/34334462/how-to-load-json-file-using-play-with-scala

    import java.io.FileInputStream
    val stream = new FileInputStream(input)
    val json_input = try {  Json.parse(stream) } finally { stream.close() }
  1. Parse json format data to send HTTP GET request According to the json-play official document, the way to parse json format data is like this.
    val storage_ip = (json_input \ "ip").get.as[String]
    val user = (json_input \ "user").get.as[String]
    val password = (json_input \ "password").get.as[String]

This example is the most simple way to retrieve value from json data. If you want to retrive data in deeper levels, let's say you want to get the name of the cluster "xbrick113", then you can write a code like this.

    val cluster_name = (json_input \ "cluster" \ 0 \ "cluster" \ "name").get.as[String]

Here, you can define the type of the value you retrieve from json by adding "as[TYPE]".

The simplest way to convert a JsValue to another type is using JsValue.asT:T.ThisrequiresanimplicitconverteroftypeReads[T]toconvertaJsValuetoT(theinverseofWrites[T]). As with Writes, the JSON API provides Reads for basic types.

Next, send HTTP GET request using XMS information. The following is an example of the function to send HTTP GET request.

  def checkXMS(storage_ip: String, user: String, password: String ) = {
    val api_path = "/api/json/v2"
    val api_url = "https://" + storage_ip + api_path
    val http_response = Http(api_url).auth(user, password)
      .option(HttpOptions.allowUnsafeSSL)
      .asString
    http_response.body
}
  1. Parse json format data in HTTP response So now you get a response from XMS server like below.
{
***** omitted *****
    "links": [
        {
            "href": "https://192.168.40.128/api/json/v2/", 
            "rel": "self"
        }
    ]
}

Let's say you want to get the value corresponding to "href" key, then, at first you need to convert the json-like formatted string into "JsValue" Type (because the type of the response is string but not json). Then, you can parse the response as json format data and retrieve the value you expect.

    val JsValue_json_string: JsValue = Json.parse(json_string)
    val parse_json_return = (JsValue_json_string \ "children" \ 0 \ "href").get

Recommended Posts

Parse Json in Scala
Read JSON in Java
POST JSON in Java
Use PostgreSQL in Scala
Create JSON in Java
POST Json in Java ~ HttpURLConnection ~
Json serialization / deserialization in Java 1.4
Do Scala Option.or Null in Java
Output Spring Boot log in json format
Try using JSON format API in Java
Working with huge JSON in Java Lambda
How to assemble JSON directly in Jackson
File output bean as JSON in spring