Skip to content

Instantly share code, notes, and snippets.

@Amerousful
Created October 7, 2024 13:56
Show Gist options
  • Save Amerousful/ef559eec70f6724838c987880e4e5871 to your computer and use it in GitHub Desktop.
Save Amerousful/ef559eec70f6724838c987880e4e5871 to your computer and use it in GitHub Desktop.
Gatling JsonPath
package utils
import com.fasterxml.jackson.databind.node.TextNode
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import io.gatling.jsonpath.JsonPath
object JsonPathUtil {
private lazy val mapper = new ObjectMapper
private def parseJson(s: String): JsonNode = mapper.readValue(s, classOf[JsonNode])
def extract(path: String, json: String) = {
JsonPath.query(path, parseJson(json)) match {
case Right(v) =>
v.toVector.map {
case text: TextNode => text.textValue()
case other: JsonNode => other.toString
}
case Left(e) => println(s"Jsonpath [$path] isn't valid : $e"); Vector()
}
}
def jsonPrettyPrint(value: String): String = {
val jsonObject = mapper.readValue(value, classOf[Any])
mapper.writerWithDefaultPrettyPrinter.writeValueAsString(jsonObject)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment