I am using JSON extractor in JMeter. Below is my Response Body. I am using the Json path expression to capture the value, which is working fine.
Apart from the above condition, I need to add one more condition.
If the "travelID" length is equal to 33, then only I need to get the BoundID.
Example : AAA-AB1234-AAABBB-2022-11-10-1111
Total length or count of the above travelID is 33, but sometime I used to get 31,32 also but I need to capture the Bound ID only when the length is 33. Is that feasible ? Please help on the same
PFB sample response body.
{
"data": {
"RenewalDetails": [
{
"ExpiryDetails": {
"duration": "xxxxx",
"destination": "XXX",
"from": "XXX",
"value": 2,
"segments": [
{
"valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
}
]
},
"Itemdetails": [
{
"BoundId": "xxx-1-xxx1-111111111111-1",
"isexpired": true,
"FamilyCode": "PREMIUM",
"availabilityDetails": [
{
"travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
"quota": "X",
"scale": "XXX",
"class": "X"
}
]
}
]
}
]
},
"warnings": [
{
"code": "xxxx",
"detail": "xxxxxxxx",
"title": "xxxxxxxx"
}
]
}
I don't think it's possible with JSON Extractor, I would rather suggest going for JSR223 PostProcessor and the following Groovy code:
def BoundId = new groovy.json.JsonSlurper().parse(prev.getResponseData())
.data.RenewalDetails[0].Itemdetails.find { itemDetail ->
itemDetail.availabilityDetails[0].travelID.length() == 33
}?.BoundId
vars.put('BoundId', BoundId ?: 'Not Found')
You will be able to refer extracted value as ${BoundId} later on where required.
I have an API response of the following structure
{
"id": "123342-123412",
"data": [
{
"id": "ace123",
"name": "Tom",
"files": [
{
"color": "yellow",
"file_id": "245"
},
{
"color": "red",
"file_id": "233"
}
]
},
{
"id": "asd123",
"name": "Jerry",
"files": [
{
"color": "red",
"file_id": "210"
},
{
"color": "green",
"file_id": "221"
}
]
},
{
"id": "acs123",
"name": "Barbie",
"files": [
{
"color": "green",
"file_id": "201"
}
]
}
]
}
I am new to ruby, I want to filter out all file ids with the color red, what's the better way of doing it rather than iterating through the whole JSON using
data.each do | object|
# individual element search code
end
I am using ruby version 2.6
The single line version that comes to my mind is:
json[:data].map {|d| d[:files] }.flatten.select {|f| f[:color] == 'red' }.map {|f| f[:file_id] }
=> ["233", "210"]
But this iterates multiple times (one for every method call), not to mention it looks kind of cryptic to me.
Personally I would prefer a verbose version, where it's clearly stated how's getting the values:
file_ids = []
json[:data].each do |data|
data[:files].each do |file|
next if file[:color] != 'red'
file_ids << file[:file_id]
end
end
file_ids.uniq # In case you have duplicates
But is up to you what to use.
I am coming to an issue where I am trying to show one object to look like my schema below. As of now, my json is returning three objects instead of returning one object. Thank you for you kind help.
"hour": [
String,
],
"week": [
String
],
"year": [
String,
],
You have 2 options:
1) Return a list with these 3 objects, as you showed
Example:
[objA, objB, objC]
2) Create a parent (wrapper) object which have a field that is a list
Example:
{items: [objA, objB, objC]}
I'm not sure if you Schema is complete. Based on what you posted, it seems wrong.
{
"id": "String",
"label": "String",
"pay_grade_description_link": "String",
"mou": "String",
"mou_description": "String",
"rate-type" : {
"hourly": [
"String"
],
"biweekly": [
"String"
],
"annual": [
"String"
]
}
}
I've been looking at the JSON1 extension for SQLite databases as a potential tool to use to compose a JSON document from a table-valued result.
I have a result set coming out of my SQLite database that resembles something like this:
CLASS|INSTANCE|PROPERTY|FROM|TO
ABC|12345|COLOR|RED|
ABC|12345|COLOR|GREEN|
ABC|12345|WEIGHT|1|10
ABC|56789|COLOR|BLUE|
ABC|56789|HEIGHT|4.5|6.2
DEF|2345|NAME|YOMOMMA|
I've been trying to make a JSON document based off of this data to look like:
{
"ABC": {
"12345": {
"COLOR": [
{ "from": "RED", "to": "" },
{ "from": "GREEN", "to": ""}
],
"WEIGHT": [
{ "from": "1", "to": "10" }
]
},
"56789": {
"COLOR": [
{ "from": "BLUE", "to": "" }
],
"HEIGHT": [
{ "from": "4.5", "to": "6.2" }
]
}
},
"DEF": {
"2345": {
"NAME": [
{ "from": "YOMOMMA", "to": "" }
]
}
}
}
I've been trying to get the JSON1 functions to help me out with this, but it seems they don't really support a multilayered JSON format (or at least, not that I've been able to see). If I wrap my result set in a group by query, I can get the properties to nicely turn into a JSON array, but when I try to wrap that into the next level, all the JSON gets escaped (would have been nice if there was a json_raw() function, but I'd imagine implementing it would be challenging).
Can this be done (relatively) easily using SQLite/ JSON1? Is there a different/ better way to create my document than using JSON1, or am I just going to have to write code for this?
I'm a beginner in ReactJS, I use react-leaflet for map rendering,
On this map I put some marker with coordinates point.
Short story, I try to get some object from JSON files, containing values by area, and coordinates points for polygon render on the map, it looks like this:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"id": 656,
"properties": {
"DCOMIRIS": "940180101",
"DEPCOM": "94018",
"NOM_COM": "Charenton-le-Pont",
"IRIS": "0101",
"TYP_IRIS": "H",
"DEP": "94",
"aire": 0.2069,
"population": 3974
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[2.4197, 48.8214],
[2.4196, 48.8205],
[2.4196, 48.8199],
[2.4196, 48.819],
[2.4196, 48.8181],
[2.4196, 48.8172],
[2.4196, 48.8169],
[2.4183, 48.8167],
[2.418, 48.8166],
[2.4166, 48.8164],
[2.4159, 48.8163],
[2.4159, 48.8163],
[2.4159, 48.8163],
[2.4155, 48.817],
[2.4152, 48.8175],
[2.4149, 48.8178],
[2.4148, 48.8181]
]
]
]
}
},
{
"type": "Feature",
"id": 657,
"properties": {
"DCOMIRIS": "940180109",
"DEPCOM": "94018",
"NOM_COM": "Charenton-le-Pont",
"IRIS": "0109",
"TYP_IRIS": "H",
"DEP": "94",
"aire": 0.4146,
"population": 3906
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[2.4055, 48.8245],
[2.4053, 48.8244],
[2.4042, 48.8235],
[2.4032, 48.8226],
[2.4024, 48.8219],
[2.4014, 48.8211],
[2.4013, 48.821],
[2.4011, 48.8209],
[2.401, 48.8207],
[2.4009, 48.8207],
[2.4009, 48.8206],
[2.4007, 48.8207],
[2.3996, 48.8212]
]
]
]
}
}
With underscore I try to get some object with coordinates value, like this:
var find = _.findWhere(this.state.data, {coordinates: [2.4055, 48.8245]});
but I got nothing, I don't know how to search "deeper" in my json.
If I try:
var find = _.findWhere(this.state.data, {id: 656});
underscore get me the object...
Any advice?
The problem you are facing is that the find method is probably comparing each of the json coordinates object, like:
"coordinates": [
[
[
[2.4055, 48.8245],
[2.4053, 48.8244],
[2.4042, 48.8235],
[2.4032, 48.8226],
[2.4024, 48.8219],
[2.4014, 48.8211],
[2.4013, 48.821],
]
]
]
With the object you provide:
"coordinates":
[2.4055, 48.8245]
And this comparison returns false.
As far as I understood you're searching for a "feature" object inside a JSON that contains coordinates [2.4055, 48.8245].
You need to do several steps to search for an element:
Loop through the features property.
Find your coordinate inside geometry.coordinates array.
The problem here could be that the coordinates array could be nested because it is a MultiPolygon object. It may be a one level deep:
[ [ 1.1, 2.2 ], [ 3.3, 4.4 ] ]
... or two and more levels deep like in your example:
[ [ [ 5.1, 6.2 ], [ 7.3, 8.4 ] ] ]
In this case you need to do a search using recursion.
Here is how this could be done with lodash or underscore (I tested with lodash).
// Recursive search function.
function find(coords, items) {
var i = 0, found;
for (; i < items.length; i++) {
if (_.isArray(items[i])) {
if (items[i].length === 2 && _.isNumber(items[i][0]) && _.isNumber(items[i][1]) && _.isEqual(items[i], coords)) {
return items[i];
} else {
found = find(coords, items[i]);
if (found) {
return found;
}
}
}
}
}
// Coordinates you're looking for
var coords = [2.4055, 48.8245];
// Loop through the features and find coordinates.
var result = _.find(_.get(data, 'features'), function (feature) {
return find(coords, _.get(feature, 'geometry.coordinates'));
});
The result is a "feature" object that contains coordinate that you're looking for.