Created
October 7, 2024 13:56
-
-
Save Amerousful/ef559eec70f6724838c987880e4e5871 to your computer and use it in GitHub Desktop.
Gatling JsonPath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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