Hello i am using Jmeters JSONPath extractor, and the i am trying to extract certain fields from a response which is structured like the JSON below. I am trying to only extract the names of the child objects and not the details inside them, so something like:
Result[0]= key1
Result[1]= key2
Result[3]= key3
Would I be able to do this using JSONPath extractor
{
"output":{
"key1":{
"field1": "value1",
"field2": "value2"
},
"key2":{
"field1": "value1",
"field2": "value2"
},
"key3":{
"field1": "value1",
"field2": "value2"
}
}
}
I don't think it's even possible with JSON Path Extractor, however it's very easy achievable with JSON JMESPath Extractor which provides keys function fully covering your use case, the relevant query is as simple as:
keys(output)
Demo:
More information: The JMeter JSON JMESPath Extractor and Assertion: A Guide
Related
Can you please let me know efficient way to extract all ItemID from below Json response and store extracted values in a variable with coma separated.
Example: JsonResponse of a request.
[
{
"ItemID": "ITM40400002",
"deliveryId": "1",
},
{
"ItemID": "ITM40400003",
"deliveryId": "2",
},
{
"ItemID": "ITM40400002",
"deliveryId": "3",
}
]
Extracted Variable
Items = ITM40400002,ITM40400003,ITM40400002
Add JSON Extractor as a child of the request which returns the above JSON
Configure it as follows:
Names of created variables: anything meaningful, i.e. foo
JSON Path Expressions: $..ItemID
Match No: -1
Tick Compute concatenation var
That's it, the JSON Extractor will extract all the ItemID attributes values and store them into foo_ALL JMeter Variable
Demo:
More information: API Testing With JMeter and the JSON Extractor
I am traying the extract the term-id- id value -200000000326 from the below response. controlled-term can have multiple term-id, I have tried with Json extractor $..controlled-term.[[term-id]].[id] and [Thursday 12:22 PM] G, Shwetha C
$..term-id[id]
but this is returning null. can you please help
enter code here:
{
"controlled-terms": {
"controlled-term": [
{
"term-id": {
"id": "200000000326"
},
"term-names": {
"term-name": [
{
"lang": "en",
"name": "All"
}
]
}
}
]
}}
For particular that JSON you provided the relevant JSONPath query would be something like:
$.controlled-term.*.term-id.id
If you need to extract particular ID which is related to some other attribute value - you will have to provide the full JSON response and specify the data relationships.
I would also recommend considering switching to JSON JMESPath Extractor mainly because it has official specification (JSONPath implementations are lacking it so query which works for one implementation may fail for another). Moreover JMESPath language is more powerful and provides a lot of functions and operators which can make your life easier. More information: The JMeter JSON JMESPath Extractor and Assertion: A Guide
I want to extract JSON value based on condition using jmeter from an HTML page.
<!doctype html>
var list_data_obj = [{
"list": [
{
"field1": "123",
"field2": "xyz"
},
{
"field1": "456",
"field2": "abc"
},
{
"field1": "789",
"field2": "asdf"
}
],
"other1": "qwerty",
"other2": "asdfgh"
}]
</html>
have tried JSON Path Extractor as below but returned NULL.
$.list[?(#.field2 == 'abc')].field1
Thanks
You won't be able to use JSON Path Extractor as response data is not a valid JSON, it looks like a mix of HTML and JavaScript therefore you will need to use Regular Expression Extractor instead.
The relevant Regular Expression will look like:
"field1": "(.+?)",\s.*"field2": "abc"
References:
JMeter: Regular Expressions
Using RegEx (Regular Expression Extractor) With JMeter
Perl 5 Regex Cheat sheet
The key is to first extract a valid json using a regular expression, then apply a json path extractor on the previously extracted json.
You can perform a 2-step extraction to do this:
First Extract the Json from the page by performing a regexp extractor with "list": ([^^]+}\]), group $1$,
Then perform a Json Extractor on the extracted variable by selecting JMeter variable, then apply your json path $.list[?(#.field2 == 'abc')].field1.
Useful resources:
How to extract data from Json with JMeter
I want to extract : children from "name":"recordInstanceId" from the following JSON.
The output should give me "lQBfjAu....P0tk" . How do I do it using Regex Extractor?
{
"name":"recordTypeView",
"attributes":{
"xmlns":""
},
"children":["all"
]
},{
"name":"isRuleBacked",
"attributes":{
"xmlns":""
},
"children":["false"
]
},{
"name":"recordInstanceId",
"attributes":{
"xmlns":""
},
"children":["lQBfjAu....P0tk"
]
}
If you need to extract lQBfjAu....P0tk from the following response:
{
"name": "recordInstanceId",
"attributes": {
"xmlns": ""
},
"children": [
"lQBfjAu....P0tk"
]
}
It could be done with JSON Path Extractor (available via JMeter Plugins) with a simple JSON Path Expression like:
$..children[0]
It isn't recommended to use Regular Expressions for dealing with JSON data, I would recommend extracting data from JSON responses with the JSON Path Extractor.
References:
JSON Path Syntax
Plugin installation instructions and XPath to JSON Path mapping
If you have any troubles - update your question to show complete response
Is there a model API I can watch/try, that has the easiest integration with RestKit (using the most defaults)?
I'm talking about the JSON format (having a root or not, nested objects, etc) and the expected answer after a POST for example...
The best resource I could find is this writeup here that teaches by example:
https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md
It would seem the basic format to represent a record from your model is something like:
{
"recordType": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
}
and if you have a collection of records, use Javascript array, like this: [ {...}, {...} ]
The writeup also suggests this form, which is:
{
"recordType": [
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
},
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
]
}
I guess you can map both ways.
This question may be helpful:
JSON format inconsistency