score:-1

If your HTTP response is quite big and you wouldn't hold it all in memory then consider using IO streams for parsing the body and serialization of the result list.

Below is an example of how it can be done with the dijon library.

  1. Add dependency to your build file:
libraryDependencies += "me.vican.jorge" %% "dijon" % "0.5.0+18-46bbb74d", // Use %%% instead of %% for Scala.js
  1. Import following packages:
import com.github.plokhotnyuk.jsoniter_scala.core._
import dijon._
import scala.language.dynamics._
  1. Parse your input stream transforming it value by value in the callback to the output stream:
val in = new java.io.ByteArrayInputStream(
"""
[
    {
        "trackerType": "WEB",
        "id": 1,
        "appId": "ap-website",
        "host": {
            "orgId": "ap",
            "displayName": "AP Mart",
            "id": "3",
            "tenantId": "ap"
        }
    },
    {
        "trackerType": "WEB",
        "id": 2,
        "appId": "test-website",
        "host": {
            "orgId": "t1",
            "tenantId": "trn11"
        }
    }
]
""".getBytes("UTF-8"))

val out = new java.io.BufferedOutputStream(System.out)

out.write('[')
scanJsonArrayFromStream[SomeJson](in) { 
  var writeComma = false
  x => 
    if (writeComma) out.write(',') else writeComma = true
    val json = obj("appId" -> x.appId, "tenantId" -> x.host.tenantId)
    writeToStream[SomeJson](json, out)(codec) 
    true
} (codec)
out.write(']')
out.flush()            

You can try it with Scastie here

When using this code in your application, you need to replace the source and destination of input and output streams.

There are other options how to solve your task. Please add more context that will help us in selection of the most simple and efficient solution.

Feel free to comment - I will be happy to help you in tuning the solution to your needs.


Related Query

More Query from same tag