Gatling: JsonPath extract multiple values - json

I'm building a gatling 2.1.3 scenario and I need to extract data from a json body.
Example of the body:
[
{
"objectId": "FirstFoo",
"pvrId": "413"
"type": "foo",
"name": "the first name",
"fooabilities": {
"foo1": true,
"foo2": true
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "firstlogo",
"width": 780,
"height": 490,
"url": "firstlogos/HD/{resolution}.png"
}
]
},
{
"objectId": "SecondFoo",
"pvrId": "414"
"type": "foo",
"name": "the second name",
"fooabilities": {
"foo1": true,
"foo2": false
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "secondlogo",
"width": 780,
"height": 490,
"url": "secondlogos/HD/{resolution}.png"
}
]
}
]
and I have this code trying to extract de data:
exec(
http("get object")
.get(commons.base_url_ws + "/my-resource/2.0/object/")
.headers(commons.headers_ws_session).asJSON
.check(jsonPath("$..*").findAll.saveAs("MY_RESULT"))) (1)
.exec(session => {
foreach("${MY_RESULT}", "result") { (2)
exec(session => {
val result= session("result").as[Map[String, Any]]
val objectId = result("objectId")
val version = result("version")
session.set("MY_RESULT_INFO", session("MY_RESULT_INFO").as[List[(String,Int)]] :+ Tuple2(objectId, version))
})
}
session
})
My goal is:
To extract the objectId and the 9th value from the version array.
I want it to look as Vector -> [(id1, version1),(id2, version2)] in the session to reuse later in another call to the API.
My concerns are:
(1) Is this going to create entries in the session with the complete sub objects? Because in other answers I was that is was always a map that was saved ("id" = [{...}]) and here I do not have ids.
(2) In the logs, I see that the session is loaded with a lot of data, but this foreach is never called. What could cause this ?
My experience in Scala is of a beginner - there may be issues I did not see.
I have looked into this issue: Gatling - Looping through JSON array and it is not exactly answering my case.

I found a way to do it with a regex.
.check(regex("""(?:"objectId"|"version"):"(.*?)",.*?(?:"objectId"|"version"):\[(?:.*?,){9}([0-9]*?),.*?\]""").ofType[(String, String)].findAll saveAs ("OBJECTS")))
I can then use this
foreach("${OBJECTS}", "object") {
exec(
http("Next API call")
.get(commons.base_url_ws + "/my-resource/2.0/foo/${object._1}/${object._2}")
[...]
}

Related

Filter to retrieve specific value in nested object using Vue

I have a nested json object:
{
"51": {
"wheels": 10,
"id": 1,
"name": "truck"
},
"55": {
"wheels": 4,
"id": 33,
"name": "Car"
},
"88": {
"wheels": 2,
"id": 90,
"name": "Bike"
}
}
I would like to filter by ID but only return the wheels so ie.
Filter ID = 33 which would return 4.
I have tried using the .filter function but I get an error: filter is not a function which I assume is because this is not an array. I have tried to replicate using answer here:
How to filter deeply nested json by multiple attributes with vue/javascript
Without success because the json has a key (51, 55, 88) so I am stumped.
Thanks for the help in advance.
You can use Object.values to convert the object into an array and then use find method on it to retrieve the specific object. Something like:
Object.values(obj).find(val => val.id === 33)?.wheels
let obj = {
"51": {
"wheels": 10,
"id": 1,
"name": "truck"
},
"55": {
"wheels": 4,
"id": 33,
"name": "Car"
},
"88": {
"wheels": 2,
"id": 90,
"name": "Bike"
}
}
console.log(Object.values(obj).find(val => val.id === 33)?.wheels)

How to retrieve nested JSON values

I have the fallowing JSON object and I want to take the value of Microsoft.VSTS.Scheduling.RemainingWork
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft.VSTS.Scheduling.RemainingWork": 32,
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
I am able retrieve data until some point:
console.log("object of json : ",result);
console.log("result[0] : ", result[0])
console.log("result[0].fields : ", result[0].fields)
The console output is,
But I this is not working result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
You can access data like an associative array :
result[0].fields['Microsoft.VSTS.Scheduling.RemainingWork']
You need to use
result[0].fields["Microsoft.VSTS.Scheduling.RemainingWork"]
Basically when you use
result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
each time you use a ".", you are trying to get the value from a nested object, like this -
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft": {
"VSTS": {
"Scheduling": {
"RemainingWork": 32
}
}
},
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
which is not correct since that is not the way your data is structured.

Want idea JSON parsing using Scala

Below is my JSON format:
{"copybook": {
"item": {
"storage-length": 1652,
"item": [
{
"storage-length": 40,
"level": "05",
"name": "OBJECT-NAME",
"display-length": 40,
"position": 1,
"picture": "X(40)"
},
{
"storage-length": 8,
"occurs-min": 0,
"level": "05",
"name": "C-TCRMANKEYBOBJ-OFFSET",
"numeric": true,
"display-length": 8,
"position": 861,
"occurs": 99,
"depending-on": "C-TCRMANKEYBOBJ-COUNT",
"picture": "9(8)"
}
],
"level": "01",
"name": "TCRMCONTRACTBOBJ",
"display-length": 1652,
"position": 1
},
"filename": "test.cbl"
}}
How can I parse this json and convert it to CSV format? I am using Scala default JSON parser. The main problem I am facing is that I can not use case class to extract the data as all the item names are not same in item array.
This format is ok for me, please follow this link and paste the JSON - https://konklone.io/json/. Any scala code is appreciated. I am getting the below data:
implicit val formats = DefaultFormats
val json2 = parse(jsonString, false) \\ "item"
val list = json2.values.asInstanceOf[List[Map[String, String]]]
for (obj <- list) {
//println(obj.keys)
//obj.values
println (obj.toList.mkString(","))
}
(name,OBJECT-NAME),(storage-length,40),(picture,X(40)),(position,1),(display-length,40),(level,05)
(name,C-TCRMANKEYBOBJ-OFFSET),(storage-length,8),(occurs-min,0),(occurs,99),(picture,9(8)),(position,861),(numeric,true),(depending-on,C-TCRMANKEYBOBJ-COUNT),(display-length,8),(level,05)
https://circe.github.io/circe/ can work better for you in terms of traversing. Just try and read.

Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes

Now I am working on extracting information from a JSON file in Ruby. Then how can I extract just the numbers next to the word 'score' from the following text file? For example, I want to get 0.6748984055823062, 0.6280145725181376 on and on.
{
"sentiment_analysis": [
{
"positive": [
{
"sentiment": "Popular",
"topic": "games",
"score": 0.6748984055823062,
"original_text": "Popular games",
"original_length": 13,
"normalized_text": "Popular games",
"normalized_length": 13,
"offset": 0
},
{
"sentiment": "engaging",
"topic": "pop culture-inspired games",
"score": 0.6280145725181376,
"original_text": "engaging pop culture-inspired games",
"original_length": 35,
"normalized_text": "engaging pop culture-inspired games",
"normalized_length": 35,
"offset": 370
},
"negative": [
{
"sentiment": "get sucked into",
"topic": "the idea of planning",
"score": -0.7923352042939829,
"original_text": "Students get sucked into the idea of planning",
"original_length": 45,
"normalized_text": "Students get sucked into the idea of planning",
"normalized_length": 45,
"offset": 342
},
{
"sentiment": "be daunted",
"topic": null,
"score": -0.5734506634410159,
"original_text": "initially be daunted",
"original_length": 20,
"normalized_text": "initially be daunted",
"normalized_length": 20,
"offset": 2104
},
What I have tried is that I could read a file and set the text file to a hash variable using the JSON method.
require 'json'
json = JSON.parse(json_string)
Using the JSON class:
Importing a file:
require "json"
file = File.open "/path/to/your/file.json"
data = JSON.load file
Optionally, you can close it now:
file.close
The file looks like this:
{
"title": "Facebook",
"url": "https://www.facebook.com",
"posts": [
"lemon-car",
"dead-memes"
]
}
The file is now able to be read like this:
data["title"]
=> "Facebook"
data.keys
=> ["title", "url", "posts"]
data['posts']
=> ["lemon-car", "dead-memes"]
data["url"]
=> "https://www.facebook.com"
Hope this helped!
Parse Data from File:
data_hash = JSON.parse(File.read('file-name-to-be-read.json'))
Then just map over the data!
reviews = data_hash['sentiment_analysis'].first
reviews.map do |sentiment, reviews|
puts "#{sentiment} #{reviews.map { |review| review['score'] }}"
end
I think this is the simplest answer.
You can use Array#map to collect the reviews.
reviews = json['sentiment_analysis'][0]
positive_reviews = reviews['positive']
negative_reviews = reviews['negative']
positive_reviews.map { |review| review['score'] }
=> [0.6748984055823062, 0.6280145725181376]
negative_reviews.map { |review| review['score'] }
=> [-0.7923352042939829, -0.5734506634410159]
Hope this helps!

Play 2.2.2 (Scala) Json parse as List[Class] issue

Given the following Json array:
{
"success": true,
"data": [
{
"id": 594,
"stage_id": 15,
"title": "test deal",
"value": 0,
"currency": "EUR",
"add_time": "2014-03-18 17:45:51",
"update_time": "2014-03-24 13:30:27",
"stage_change_time": "2014-03-24 13:30:27",
"active": true,
"deleted": false,
"status": "open",
"expected_close_date": null,
"stage_order_nr": 1,
"person_name": "test"
},
{
"id": 601,
"stage_id": 15,
"title": "test deal2 deal",
"value": 0,
"currency": "EUR",
"add_time": "2014-03-24 14:11:00",
"update_time": "2014-03-24 14:11:00",
"stage_change_time": "2014-03-24 14:11:00",
"active": true,
"deleted": false,
"status": "open",
"expected_close_date": null,
"stage_order_nr": 1,
"person_name": "test deal2"
}
],
"additional_data": {
"pagination": {
"start": 0,
"limit": 100,
"more_items_in_collection": false
}
}
}
I want to get a List of deals out of it and I am trying it like so
case class Deal(id: Long, stage_id: Long)
def getAllDeals(): List [Deal] = {
var holder : WSRequestHolder = WS.url(PipeDriveApiBaseUrl + "/deals")
val complexHolder: WSRequestHolder = holder.withQueryString("filter_id" -> "9", "api_token" -> SalesManagerApiKey)
val futureResponse: Future[Response] = complexHolder.get()
implicit val dealReader = Json.reads[List[Deal]]
val futureJson: Future[List[Deal]] = futureResponse.map(
response => (response.json \ "data").validate[List[Deal]].get
)
I get the exception No unapply function found which is related to the implicit reads value. But commenting it out, I will get No Json deserializer found for type List[models.Deal]. Try to implement an implicit Reads or Format for this type.
I couldn't solve the problem with these answers here and here.
What do I miss or misunderstand?
Instead of defining an implicit Json.reads for List[Deal], create one for Deal:
implicit val dealReader = Json.reads[Deal]
Play already has a built-in implicit JSON reads converter for Lists. Actually, it has one for all Traversables. You can look at the code, but it's a bit hard to read. The thing is: Play can convert JSON lists to List objects. What it doesn't know is how to read/convert a Deal, and that's why you need the implicit definition mentioned above.