Robot Framework how to count item list in JSON - json

I would like to count item "Start" from JSON APIs on Robot Framework
{
"result": {
"api": "xxx",
"timestamp": "14:41:18",
"series": [
{
"series_code": "test",
"series_name_eng": "test",
"unit_eng": "t",
"series_type": "e",
"frequency": "s",
"last_update_date": "2020",
"observations": [
{
"start": "2020-01",
"value": "999"
},
{
"start": "2020-02",
"value": "888"
},
{
"start": "2020-03",
"value": "777"
},
]
}
I use this not working
${json_string} Get File ./example.json
${json_object} evaluate json.loads('''${json_string}''') json
#${value}= get value from json ${json_object} $.result.series[0].observations
${x_count} Get Length ${json_object["$.result.series[0].observations"]}
Could you please help guide to for how?

The Json provided in the example above is not valid one. That will need to be fixed by closing series array ] then close the results object } and then close the outer object }
Valid json will look like this -
${Getjson}= {"result":{"api":"xxx","timestamp":"14:41:18","series":[{"series_code":"test","series_name_eng":"test","unit_eng":"t","series_type":"e","frequency":"s","last_update_date":"2020","observations":[{"start":"2020-01","value":"999"},{"start":"2020-02","value":"888"},{"start":"2020-03","value":"777"}]}]}}
You were close with this jsonpath $.result.series[0].observations. The correct one is in below example -
${json}= Convert String to JSON ${Getjson}
#{Start}= Get Value From Json ${json} $.result.series[?(#.observations)].observations[?(#.start)].start
${length} Get length ${Start}
log ${length}
Output:-

Related

could not fetch json object using jsonpath

I've a requirement to fetch value from json using jayway jsonpath.
Json structure looks like below
[
{
"type": "a",
"values": [
{
"name": "a",
"value": [1,2,3]
},
{
"name": "b",
"value": [3,4,5]
},
{
"name": "c",
"value": [6,7,8]
}
]
}
]
my requirement is in the values array if the name value is a and value array contains value 1, then I need to fetch value array where name is b.
I have written jsonPath expression like below
$..values[?(#.name == 'a')]
where it is returning only
{
"name": "a",
"value": [1,2,3]
}
could someone help me in writing jsonpath expression please , Thanks in advance.
expected output
[3,4,5]
tried with
$..[?(#.values[?(#.name== 'a' && #.value CONTAINS 1)])]
then it is matching every object present in root array.
With Jayway JSONPATH you might get lucky with below jsonpath
$..[?(#.values[?(#.name=='a')].value[*] contains 1 )].values[?(#.name=='b')].value[*]

How to parse this boolean contained JSON output with jq?

The JSON output I am trying to parse:
{
"success": true,
"data": {
"aa": [
{
"timestamp": 123456,
"price": 1
},
{
"timestamp": 123457,
"price": 2
],
"bb": [
{
"timestamp": 123456,
"price": 3
},
{
"timestamp": 123457,
"price": 4
}
]
}
}
So after banging my head against the wall a million times, I just removed the "success": true", line from the output and I could easily do jq stuff with it. Otherwise if I ran for example:
cat jsonfile.json | jq -c .[].aa
I would get:
Cannot index boolean with string "aa"
Which makes sense, since the first key is boolean. But I have no clue how to skip it while processing with jq.
Goal is to filter only timestamp and price of "aa", without giving any care about the "success": true key/value pair.
You need to select the data field first: jq .data.aa[]

How to write JSON Path to get particular value based on string filter?

I am new to JSON Path and I am trying to get 'id' corresponding to name='Candy' using JsonPath in below JSON payload.
{
"responsePayload": [
{
"content": {
"id": "101",
"name": "Candy"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/101",
"id": "101"
}
]
},
{
"content": {
"id": "102",
"name": "Chocolate"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/102",
"id": "102"
}
]
}
]
}
For this I tried Jsonpath $.responsePayload[0].content[?(#.name=='Candy')].id but that is not working. I am using https://jsonpath.com/ for testing this JsonPath. Please guide me how I can achieve required result.
You're really close. You need more of the path inside the expression.
$.responsePayload[?(#.content.name=='Candy')].content.id
The [0] you have in your response isolates the first element only, but it sounds like you want to iterate over the whole array in responsePayload. To do that, the filter expression selector [?(...)] must act on that array.
Next, the # represents the current item, and you can build a full path off of it. You can see how that works above.
Finally, the filter expression selector returns the full item when the expression returns true. So you then need to navigate into .content.id again to get the value you're after.

jmeter: I want to extract 2 values from 2 different json objects where 1 json object will validate my condition & other value I want to extract

My JSON Response is :
{
"results": [
{
"attributes": [
{
"format": "internal",
"name": "resourceid",
"type": "STRING",
"value": "56B15190000015E85E57923F0000033B"
},
{
"format": "attribute",
"name": "ds6w:identifier",
"type": "string",
"value": "ald7_al"
}
]
},
{
"attributes": [
{
"format": "internal",
"name": "resourceid",
"type": "STRING",
"value": "56B15190000015E85E578B1F000001B6"
},
{
"format": "attribute",
"name": "ds6w:identifier",
"type": "string",
"value": "fbh1"
}
]
},
{
"attributes": [
{
"format": "internal",
"name": "resourceid",
"type": "STRING",
"value": "56B15190000015E85E578F7800000211"
},
{
"format": "attribute",
"name": "ds6w:identifier",
"type": "string",
"value": "u89cf"
}
]
}
]
}
I want to get '56B15190000015E85E57923F0000033B' where value='ald7_al'
So basically within a jsonarray I have jsonobjects, and for single jsonobject I have two jsonobjects where secong jsonobject will validate my condition param and I want value from first jsonobject
For getting result to solve condition check I have used
JSON extractor expression as -> $..attributes[?(#.value==ald7_al)] which is giving me second json block but I want value from first json block.
Please help me if you have any inputs.
Thanking you in advance for your help!
As of JMeter 5.2.1 this it not possible with built-in JMeter components
JSON Extractor cannot do this due to underlying Jayway JsonPath issue 287
JSON JMESPath Extractor cannot do this due to underlying JMESPath issue 22
So you're left with JSR223 PostProcessor and Groovy language, example code which should resolve your issue would be something like:
def results = new groovy.json.JsonSlurper().parse(prev.getResponseData()).results
0.upto(results.size() - 1, { index ->
def attributes = results[index].attributes
if (attributes[1].get('value').equals('ald7_al')) {
vars.put('value', attributes[0].get('value'))
}
})
Add it as a child of the request which returns the above JSON and if everything goes well you will able to access the value you're looking for as ${value} where required.
More information:
Apache Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Parsing and cleaning text file in Python?

I have a text file which contains raw data. I want to parse that data and clean it so that it can be used further.The following is the rawdata.
"{\x0A \x22identifier\x22: {\x0A \x22company_code\x22: \x22TSC\x22,\x0A \x22product_type\x22: \x22airtime-ctg\x22,\x0A \x22host_type\x22: \x22android\x22\x0A },\x0A \x22id\x22: {\x0A \x22type\x22: \x22guest\x22,\x0A \x22group\x22: \x22guest\x22,\x0A \x22uuid\x22: \x221a0d4d6e-0c00-11e7-a16f-0242ac110002\x22,\x0A \x22device_id\x22: \x22423e49efa4b8b013\x22\x0A },\x0A \x22stats\x22: [\x0A {\x0A \x22timestamp\x22: \x222017-03-22T03:21:11+0000\x22,\x0A \x22software_id\x22: \x22A-ACTG\x22,\x0A \x22action_id\x22: \x22open_app\x22,\x0A \x22values\x22: {\x0A \x22device_id\x22: \x22423e49efa4b8b013\x22,\x0A \x22language\x22: \x22en\x22\x0A }\x0A }\x0A ]\x0A}"
I want to remove all the hexadecimal characters,I tried parsing the data and storing in an array and cleaning it using re.sub() but it gives the same data.
for line in f:
new_data = re.sub(r'[^\x00-\x7f],\x22',r'', line)
data.append(new_data)
\x0A is the hex code for newline. After s = <your json string>, print(s) gives
>>> print(s)
{
"identifier": {
"company_code": "TSC",
"product_type": "airtime-ctg",
"host_type": "android"
},
"id": {
"type": "guest",
"group": "guest",
"uuid": "1a0d4d6e-0c00-11e7-a16f-0242ac110002",
"device_id": "423e49efa4b8b013"
},
"stats": [
{
"timestamp": "2017-03-22T03:21:11+0000",
"software_id": "A-ACTG",
"action_id": "open_app",
"values": {
"device_id": "423e49efa4b8b013",
"language": "en"
}
}
]
}
You should parse this with the json module load (from file) or loads (from string) functions. You will get a dict with 2 dicts and a list with a dict.