Jmeter Json Extractor with multiple conditional - failed - json

I am trying to create a Json Extractor and it`s being a thought activity. I have this json structure:
[
{
"reportType":{
"id":3,
"nomeTipoRelatorio":"etc etc etc",
"descricaoTipoRelatorio":"etc etc etc",
"esExibeSite":"S",
"esExibeEmail":"S",
"esExibeFisico":"N"
},
"account":{
"id":9999999,
"holdersName":"etc etc etc",
"accountNamber":"9999999",
"nickname":null
},
"file":{
"id":2913847,
"typeId":null,
"version":null,
"name":null,
"format":null,
"description":"description",
"typeCode":null,
"size":153196,
"mimeType":null,
"file":null,
"publicationDate":"2018-12-05",
"referenceStartDate":"2018-12-05",
"referenceEndDate":"2018-12-06",
"extension":null,
"fileStatusLog":{
"idArquivo":2913847,
"dhAlteracao":"2018-12-05",
"nmSistema":"SISTEMA X",
"idUsuario":999999,
"reportStatusIndicador":"Z"
}
}
}
]
What I need to do: First of all, I am using the option "Compute concatenation var" and "Match No." as -1. Because the service can bring in the response many of those.
I have to verify, if "reportStatusIndicador" = 'Z' or 'Y', if positive, I have to collect File.Id OR file.FileStatusLog.idArquivo, they are the same, I was trying the first option, in this case the number "2913847", but if come more results, I will collect all File.id`s
With this values in hands, I will continue with a for each for all File.id`s.
My last try, was this combination, after reading a lot and tried many others combinations.
[?(#...file.fileStatusLog.reportStatusIndicador == 'Z' || #...file.fileStatusLog.reportStatusIndicador == 'Y')].file.id
But my debug post processor always appears like this, empty:
filesIds=

Go for $..[?(#.file.fileStatusLog.reportStatusIndicador == 'Z' || #.file.fileStatusLog.reportStatusIndicador == 'Y')].file.id
Demo:
References:
Jayway JsonPath: Inline Predicates
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

I could do it with this pattern:
[?(#.file.fileStatusLog.reportStatusIndicador == 'Z' ||
#.file.fileStatusLog.reportStatusIndicador == 'Y')].file.id
filesIds_ALL=2913755,2913756,2913758,2913759,2913760,2913761,2913762,2913763,2913764,2913765,2913766,2913767,2913768,2913769,2913770

Related

Jmeter - extract json data from dynamic payload using 'and' statement

I want to extract value from dynamic JSON.
This generation is different every time when is executed.
I need to get ex: XLIfccMNLv1asVam3QuatowCmrp8IYuE0FUDMYncegs=
which is generated in the different location in the Json file, with different value
I tried with.
$.payload[?(#.eventType == 'AAA')].entityId
which is working fine.
But, i want more stronger query.
Is it possible to use && statement with the query something like:
$.payload[?(#.eventType == 'AAA')&&(#.outgoingCurrency== 'EUR')].entityId
My payload:
{
"payload":[
{
"entityId":"qvr_IlDhTdzldeccxguNR84sE0N78DUfNGzwH-3pY7Y=",
"accountHolderId":"dvwxpTxVHdo2n1d5ytO6WyhnI2nuaEuzsh47agPpSFU=",
"processorType":"DUMMY",
"eventType":"AAA",
"outgoingCurrency":"USD",
"holdPeriodInHours":11,
"disabled":false
},
{
"entityId":"XLIfccMNLv1asVam3QuatowCmrp8IYuE0FUDMYncegs=",
"accountHolderId":"Xoo8uAM90qRT7kceDUJBIIqafUuUdH2fH_Ia2z1TY5w=",
"processorType":"DUMMY",
"eventType":"BBB",
"outgoingCurrency":"EUR",
"holdPeriodInHours":10,
"disabled":false
},
{
"entityId":"yBoHvYkyszaQpaFe1zvqCY416_vYiq7iivA9bWJhiTg=",
"processorType":"BMO_CPR",
"eventType":"AAA",
"disabled":false
}
]
}
You need to use the operation && inside the expression:
$.payload[?(#.outgoingCurrency== 'EUR' && #.eventType == 'AAA')].entityId.
For more details see: https://goessner.net/articles/JsonPath/

What's the correct JsonPath expression to search a JSON root object using Newtonsoft.Json.NET?

Most examples deal with the book store example from Stefan Gössner, however I'm struggling to define the correct JsonPath expression for a simple object (no array):
{ "Id": 1, "Name": "Test" }
To check if this json contains Id = 1.
I tried the following expression: $..?[(#.Id == 1]), but this does find any matches using Json.NET?
Also tried Manatee.Json for parsing, and there it seems the jsonpath expression could be like $[?($.Id == 1)] ?
The path that you posted is not valid. I think you meant $..[?(#.Id == 1)] (some characters were out of order). My answer assumes this.
The JSON Path that you're using indicates that the item you're looking for should be in an array.
$ start
.. recursive search (1)
[ array item specification
?( item-based query
#.Id == 1 where the item is an object with an "Id" with value == 1 at the root
) end item-based query
] end array item specification
(1) the conditions following this could match a value no matter how deep in the hierarchy it exists
You want to just navigate the object directly. Using $.Id will return 1, which you can validate in your application.
All of that said...
It sounds to me like you want to validate that the Id property is 1 rather than to search an array for an object where the Id property is 1. To do this, you want JSON Schema, not JSON Path.
JSON Path is a query language for searching for values which meet certain conditions (e.g. an object where Id == 1.
JSON Schema is for validating that the JSON meet certain requirements (your data's in the right shape). A JSON Schema to validate that your object has a value of 1 could be something like
{
"properties": {
"Id": {"const":1}
}
}
Granted this isn't very useful because it'll only validate that the Id property is 1, which ideally should only be true for one object.

(Python)How to process complicated json data

//Please excuse my poor English.
Hello everyone, I am doing a project which is about a facebook comment spider.
then I find the Facebook Graph GUI. It will return a json file that's so complicated for me.
The json file is include so many parts
then I use json.loads to get all the json code
finally it return a dict for me.
and i dont know how to access the Value
for example i want get all the id or comment.
but i can only get the 2 key of dict "data" and "pading"
so, how can i get the next key? like "id" or "comment"
and how to process this complicated data.
code
Thank you very much.
Two ways I can think of, either you know what you're looking for and access it directly or you loop over the keys, look at the value of the keys and nest another loop until you reach the end of the tree.
You can do this using a self-calling function and with the appropriate usage of jQuery.
Here is an example:
function get_the_stuff(url)
{
$.getJSON(url, function ( data ) {
my_parser(data) });
}
function my_parser(node)
{
$.each(node, function(key, val) {
if ( val && typeof val == "object" ) { my_parser(val); }
else { console.log("key="+key+", val="+val); }
});
}
I omitted all the error checking. Also make sure the typeof check is appropriate. You might need some other elseif's to maybe treat numbers, strings, null or booleans in different ways. This is just an example.
EDIT: I might have slightly missed that the topic said "Python"... sorry. Feel free to translate to python, same principles apply.
EDIT2: Now lets' try in Python. I'm assuming your JSON is already imported into a variable.
def my_parser(node, depth=0):
if type(node) == "list":
for val in node:
my_parser(val,depth+1)
elif type(node) == "dict":
for key in node:
printf("level=%i key=%s" % ( depth, key ))
my_parser(node[key], depth+1)
elif type(node) == "str":
pritnf("level=%i value=%s" % ( depth, node ))
elsif type(node) == "int":
printf("level=%i value=%i % ( depth, node ))
else:
printf("level=%i value_unknown_type" % ( depth ))

JMESPath JSON filter with multiple matches

I have a json block that looks a bit like this (have you guessed from AWS)
{ "Vpcs":[
{
"VpcId":"vpc-blabla1",
"OtherKey":"Value"
},
{
"VpcId":"vpc-blabla2",
"OtherKey":"Value"
},
{
"VpcId":"vpc-blabla3",
"OtherKey":"Value"
},
{
"VpcId":"vpc-blabla4",
"OtherKey":"Value"
}]
}
I want to use JMESPath to get the OtherKey value for vpc-blabla1 and vpc-blabla3 (Examples, could be any list of vpc-id)
I can get blabla1 with JMESpath filter
Vpcs[?VpcId=='blabla1'].OtherKey
But I can't find the syntax for multiple values? I have tried the Or syntax || and the composite syntax | but neither works? - See below for things I have tried.
Vpcs[?VpcId=='blabla1' || 'blabla1'].OtherKey
Vpcs[?VpcId=='blabla1' || ?VpcId=='blabla1'].OtherKey
Vpcs[(?VpcId=='blabla1') || (?VpcId=='blabla1')].OtherKey
Vpcs[?VpcId=='blabla1' | ?VpcId=='blabla1'].OtherKey
Any suggestions? Is this possible or am I going to have to gather one result set at a time and recombine the results I want?
The general syntax for multiple is [? expr1 || expr2] so in your case, you can use:
Vpcs[?VpcId=='vpc-blabla1' || VpcId=='vpc-blabla2'].OtherKey
Another option, if you have many VPC ids you're search for, you can also say:
Vpcs[?contains(`["vpc-blabla1", "vpc-blabla2"]`, VpcId)].OtherKey

JsonPath: Selecting root level field if satisfies a condition

Given the below Json input:
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
I need to select the author field if the author matches a given name for eg. Evelyn Waugh. I am struggling to write the JsonPath Expression for this. I tried the following with no success. Can anyone suggest the correct expression?
$.author?(# == 'Evelyn Waugh')
$.?(#.author == 'Evelyn Waugh')
$..?(#.author == 'Evelyn Waugh')
try $.[?($.author=='Evelyn Waugh')]
For me #Bernie's answer seems to work:
$[?($.author == 'Evelyn Waugh')]
It works on 3/4 json path executors (unfortunately the one not working is jsonpath.com).
The ones working are:
https://codebeautify.org/jsonpath-tester
https://jsonpath.herokuapp.com/
WireMock (using jsonpath in java)
For those who are wondering as to why is this used and why not a plain comparison is being done here, please continue reading.
In my case I am using wiremock to simulate different server responses based on different input values of json, so for example when "author" is "Evelyn Waugh" then reply with {"books":"10"} and for all other values of "author" reply with, maybe {"books":"20"}
So the option here is only to match with a json path and this is what did the trick.
For those still wondering note that somehow wiremock doesn't work with if and else conditions, if you rely on the else condition (wildcard match) it precedes all the conditions.
I suppose you could do this:
$[?($.author === # && # == 'Evelyn Waugh')]
But that's a terribly useless query and error prone if you'd ask me. It all goes to hell if there's another property with the author's name.
Frankly, you should just get the author and test if it's the name you're expecting. Don't try to force this to work, it's not meant to be used this way.
Add your root object into an array before executing the query.
Then this should work.
'$..[?(#.author == "Evelyn Waugh")]'
With jsonpath
import jp from 'jsonpath';
const queryResults = jp.query([yourObject], '$..[?(#.author == "Evelyn Waugh")]');