0

I have a JSON array containing the following details, I would like to extract the text Alignment value of Right and assign it to a val.

"data":[
  {
    "formatType": "text",
    "value": "bgyufcie huis huids hufhsduhfsl hd"
  },
  {
    "formatType": "text size",
    "value": 12
  },
  {
    "formatType": "text alignment",
    "value" : "right"
  }
]

Any thoughts?

5 Answers 5

3

Using the Gson library, you can map the json in a Java object.

So, you have to create a class like this:

public class MyObject{
    private String formatType;
    private String value;

    //Constuctors, Getter and Setter...
    //.....
    //.....
}

After, using the method fromJson you can create an array of MyObject.

Gson gson = new Gson();
MyObject[] array = gson.fromJson(new FileReader("file.json"), MyObject[].class);
Sign up to request clarification or add additional context in comments.

Comments

3

You can also use json4s library as shown next:

import org.json4s._
import org.json4s.jackson.JsonMethods._

val json = """{
     "data":[
            {
                "formatType": "text",
                "value": "bgyufcie huis huids hufhsduhfsl hd"
            },
            {
                "formatType": "text size",
                "value": 12
            },
            {
                "formatType": "text alignment",
                "value" : "right"
             }
             ]
}"""

val parsed = parse(json)

val value = (parsed \ "data" \\ classOf[JObject]).filter(m => m("formatType") == "text alignment")(0)("value")

// value: Any = right

The filter (parsed \ "data" \\ classOf[JObject]) extracts all the items into a List of Map i.e:

List(
   Map(formatType -> text, value -> bgyufcie huis huids hufhsduhfsl hd), 
   Map(formatType -> text size, value -> 12), Map(formatType -> text alignment, value -> right)
).

From those we apply the filter filter(m => m("formatType") == "text alignment") to retrieve the record that we really need.

8 Comments

I tried this, but am getting an error saying val parsed = parse (json) required json input , found JSArray
parse(....) accepts a string json are sure you are passing a string in the function? The error says that json is not a string. Also make sure that the json is enclosed with brackets as shown above
@ShanMarshBubashan are you looking for a java or scala solution?
am searching for a scala solution, And I am passing a Option[JsArray] inside parse
Here you can find the definition of parse(). You must pass a string and not Option[JsArray]! BTW why Option[JsArray]? How did you come up with Option[JsArray]?
|
2

Use Dijon FTW!

Here is a test that demonstrates how easily the "right" value can be found in samples like yours:

import com.github.pathikrit.dijon._

val json = parse(
  """{
    |"data":[
    |  {
    |    "formatType": "text",
    |    "value": "bgyufcie huis huids hufhsduhfsl hd"
    |  },
    |  {
    |    "formatType": "text size",
    |    "value": 12
    |  },
    |  {
    |    "formatType": "text alignment",
    |    "value" : "right"
    |  }
    |]
    |}""".stripMargin)

assert(json.data.toSeq.collect { 
  case obj if obj.formatType == "text alignment" => obj.value 
}.head == "right")

Comments

2

I would use the Jackson library, it is very helpful for parsing JSON. You can read the JSON using an ObjectMapper.

Here is a full tutorial to get you started: https://www.mkyong.com/java/jackson-how-to-parse-json/

Comments

0

create a multiline JSON string, then parse that string directly into a Scala object, use the net.liftweb package to solve this.

import net.liftweb.json._

object SarahEmailPluginConfigTest {

implicit val formats = DefaultFormats
case class Mailserver(url: String, username: String, password: String)

val json = parse(
"""
{ 
  "url": "imap.yahoo.com",
  "username": "myusername",
  "password": "mypassword"
}
"""
)

  def main(args: Array[String]) {
    val m = json.extract[Mailserver]
    println(m.url)
    println(m.username)
    println(m.password)
  }

}

https://alvinalexander.com/scala/simple-scala-lift-json-example-lift-framework

Reference link

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.