I’ve been looking at the jsonUrl feeder example and it assumes the JSON is already an array. What would I need to do if the response isn’t an array, but contains an array.
For example, the response from the webservice looks similar to:
{ "foo":"bar", "listOfStuff":[ {"stuff1":"value1"}, {"stuff2":"value2"}, {"stuff3":"value3"} ] }
I’d like my jsonUrl to iterate over “listOfStuff.”
I looked at the docs for passing a PartialFunction to the convert() function, but I’m not sure what the type of the parameters passed into the PartialFunction would be. A string of the key and an object representing the value?
Would I effectively do something like this?:
case ("listOfStuff", object) => object
As you said, the JsonFeeder expects you to provide an array. Maybe we could provide a way to pass a custom parsing logic function.
Until then, just fork it and have the JSON parsing match your specific needs: https://github.com/gatling/gatling/blob/2.1.X/gatling-core/src/main/scala/io/gatling/core/feeder/JsonFeederFileParser.scala
Ok, thanks for the quick response!
I haven’t had a chance to work this into a PartialFunction implementation, but I was able to code a work around directly into my scenario. If anyone else has a similar need, here’s how I called a REST endpoint that returned a JSON document with a sub-array that I wanted to use in my feeder (it’s very narrow in that it only supports a sub-array one level down):
`
// Returns an array of JSON elements for a URL
def jsonUrl(url: String) = RecordSeqFeederBuilder(getUrl(url))
// Returns a JSON parsed object from a URL
def getUrl(url: String): IndexedSeq[Record[Any]] =
withCloseable(new URL(url).openStream) { is =>
stream(is).toVector
}
// Parses the response from an input stream into a JSON object
def stream(is: InputStream): Iterator[Record[Any]] = {
Jackson.parse(is, configuration.core.charset) match {
// Pulls just the “summaries” array out of a recent orders query
// This case statement is bit that deviates from the stock code… everything else is just glue
case map: JMap[_, ] => // Retrieves the summaries array
map.get(“summaries”) match {
case array: JCollection[] =>
array.iterator.collect {
case element: JMap[_, _] =>
element.asInstanceOf[JMap[String, _]].toMap
}
}
case _ => throw new IllegalArgumentException(“Unable to retrieve an array of elements for the feeder”)
}
}
// Example usage…
val userIdJsonFeeder = jsonUrl(“http://myurl/foo.json”)
.convert {
case (“summaries”, summaries) => summaries
}.random
`