-3

I have newly started working on scala. I have following json array:

[
  {
     "id": "1",
    "formatType": "text",
    "value": "bgyufcie huis huids hufhsduhfsl hd"
  },
  {
    "id": "2",
    "formatType": "text size",
    "value": 12
  },
  {
    "id": "3",
    "formatType": "text alignment",
    "value" : "right"
  }
]

I am trying to retrieve json from this array on the basis of id. For example, if id is 2 then I want to retreive following

 {
        "id": "2",
        "formatType": "text size",
        "value": 12
      }

and so on for other id's. I have written a code which compares id and return me json which is as follows.

val getid = jsonString.parseJson match {
      case JsArray(elements) => elements.map(x => if(x.asJsObject().getFields("Id")(0).toString().replace("\"", "") == key) x)
    }

This code works fine when in json array I have only one json. But when I have multiple json with id's as shown above, this code only compares the last record. That is in this case it is only comparing data with id 3. It is not comparing with id 1 and and id 2 and because of which I am not able to get desired results. I tried using for each here but that didn't worked for me. for each prints full data in characters. How can I check all records in my json array and match id and return it?

8
  • 1
    Hello @Lozy and welcome to Stackoverflow. I have noticed that you have asked the same question now for the third time. Please edit your questions and improved it before creating a new account and duplicate your questions. This is discouraging for everyone that is trying to help you. Other two questions are: here and here Commented Aug 1, 2020 at 8:28
  • No I have moved forward. I found a solution and proceeded and stuck somewhere else. Same language is used to give background of the problem Commented Aug 1, 2020 at 8:30
  • In relation to your actual problem with parsing a JSON with Scala. You can rest assured that there are enough answers on Stackoverflow or elsewhere on the web so this kind of question will most likely attract more downvotes or will be marked as duplicate. Commented Aug 1, 2020 at 8:30
  • 1
    Okay, fair point. I see that there is a slight difference in each of the questions. But I am sure others will not read as carefully as I did now and will probably either ignore or downvote this question. And from a personal perspective, as soon as someone creates a new account, that already looks suspicious to me. I am really trying to help you and get somebody to answer your question. Commented Aug 1, 2020 at 8:34
  • 1
    In addition, if you find an answer helpful (like the one from ForeverLearner) in the orther post, it is good practice to also appreciate it by accepting the answer. Commented Aug 1, 2020 at 8:40

1 Answer 1

1

I agree with the comments about not asking multiple questions, and looking at existing answers (and accepting correct answers!).

However in this particular case you just need to use find rather than map:

val getid = jsonString.parseJson match {
  case JsArray(elements) =>
    elements.find(_.asJsObject().getFields("Id")(0).toString().replace("\"", "") == key)
  case _ =>
    None
}

This will return None if the id is not found or the JSON is not a JsArray, otherwise it will return Some(element)

More generally, rather than processing the raw JSON objects, I would recommend using a library that converts the whole JSON to Scala objects and then processing those Scala objects.

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.