JMeter: JSON path expressions - Extract JSON blocks using multiple conditions - json

I want to extract JSON block where it satisfies multiple conditions. For example, extract a block which has variables with two or more desired value. Please see below given example.
[
{
id:"1",
name:"ABC - Project 1",
appName:"XYZ",
state:"New",
appType:"owner",
date:"May 12"
},
{
id:"2",
name:"DEF - Project 2",
appName:"UVW",
state:"In Progress",
appType:"manager",
date:"May 13"
},
{
id:"3",
name:"GHI - Project 3",
appName:"RST",
state:"In Progress",
appType:"owner",
date:"May 12"
},
{
id:"4",
name:"JKL - Project 4",
appName:"OPQ",
state:"Expired",
appType:"entity owner",
date:"July 13"
}
]
From the above JSON, I want to extract the JSON block where state:"In Progress" or state:"New", either of these states and it should of appType:"Owner" along with name:... Project 1; i.e. the following blocks as output:
{
id:"1",
name:"ABC - Project 1",
appName:"XYZ",
state:"New",
appType:"owner",
date:"May 12"
}
Which JSON Path expressions whould I use to extract:
$.[?((#.state == "In Progress" || #.state == "New") && #.appType== "owner" && <some regex expression>)]
But it doesn't extract any result. Is there any way to use multiple conditions for "OR/||" and "AND/&&" condition to extract that particular block. Please help!
Thanks,
Sid

Your expression works fine for me:
So you might want to double check:
jmeter.log file for any suspicious entries like Error processing JSON content in JSON Extractor
the values of the JMeter Variables as depending on your JSON Extractor configuration the values may be written into variables with indices, i.e. you expect FOO but you're getting FOO_1 and FOO_2, it can be done using Debug Sampler and View Results Tree listener combination

Related

Is there a Google Sheets or MS Excel formula to convert a column with JSON array to multiple rows

I'm trying to parse a column with JSON array data in an Excel file. I want to convert that JSON array to multiple rows.
I've tried using split text to columns, but that is giving inconsistent results.
Input:
Expected Output:
You can find my input and expected_output sheet in this Google sheet
https://docs.google.com/spreadsheets/d/1zohaZXEoppbn4GBq-BLEFHk5n4PPwYPOTnbZbq6YbNs/edit?usp=sharing
there are many ways one of which is:
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(C2:C, ","))<>"", "♦"&A2:A&"♠"&B2:B&"♠"&
REGEXEXTRACT(SPLIT(SUBSTITUTE(C2:C, """", ), ","), ":(\d+)"), ))
,,999^99)),,999^99)), "♦")), "♠"))
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(LEN(A2), "♦"&A2&"♠"&B2&QUERY(TRANSPOSE("♠"&QUERY(REGEXEXTRACT(SPLIT(
TRANSPOSE(SPLIT(SUBSTITUTE(C2, """", ), "{")), ","), ":(\d+)"), "offset 1", 0))
,,999^99), )),,999^99)),,999^99)), "♦")), "♠"))
One may adopt the code from my project.
json is a tricky format, it uses different data types and also may contain nested items:
{ key: { key1: "a", key2: "b" } }
This is the reason to use google-apps-script for the task.
Please, copy the sample file:
or see the source code here:
https://github.com/Max-Makhrov/GoogleSheets/blob/master/Json.gs
Sample usage:
=getJsonArrayAsTable(C2)
I needed to do something similar today in Google Sheets. The solution I came up with is surprisingle simple. It may be useful but has a couple of dependencies:
The JSON data conforms to a Google Apps Script Range-like structure (examples below)**.
A custom function defined using Google Apps Script.
The custom function
function JSONTOJS(JSONString) {
return JSON.parse(JSONString);
}
Sample Data
A simple list:
["Option 1", "Option 2", "Option 3", "Option 4"]
Tabular data:
[["Title 1", "Title 2", "Title 3", "Title 4"], ["Option 1", "Option 2", "Option 3", "Option 4"], ["Option 5", "Option 6", "Option 7", "Option 8"]]
Key:value pairs:
[["key1","value 1"], ["key2","value 2"], ["key3","value 3"]]
In use:
Assuming one of these JSON strings was in cell A1, in some other cell input the following formula and the data will be displayed in the cells in your Sheet.
=JSONTOJS(A1)
Example Sheet (get a copy of it)
** If you're defining the data structure, this may be useful. You're unlikely to receive data from some service in this format (it is quite Google Sheets unique), in which case your mileage will vary.
google-apps-script

How to select objects with specific IDs from a JSON array with jq?

I have a JSON file containing an array of objects (test.json):
[
{
"name": "Test 1",
"id": 1
},
{
"name": "Test 2",
"id": 2
},
{
"name": "Test 3",
"id": 3
}
]
I want to extract all objects, that have a certain ID. I managed to get an object if I want just one specific ID: jq 'map(select(.id == 2 ))' test.json.
Thing is, I have a list of IDs, say 1 and 3. How do I get a list containing only those object? So in this example a list containing the objects with ID 1 and 3?
You can check the example here: https://jqplay.org/s/xQgpA4yJAz
jq 'map(select(.id | contains(1,3)))'
Man, jq is so great
https://github.com/stedolan/jq/wiki/Cookbook#filter-objects-based-on-the-contents-of-a-key
The solution using contains/1 as presented on this page could just as well be written using ==:
map(select(.id == (1,3)))
The main reason for mentioning this is that contains is full of potential surprises. (Consider, for example, what would happen if .id were string-valued.)
Unfortunately, using either == or contains as above is computationally inefficient (it is O(m*n)), though in practice it is quite fast.

Using logical operators in json path file

Is there a functionality to use logical operators in JSON path file used in copy command.
For example, I have a JSON which can contain a key which can either be
Desc
Or
Description
So in the JSON it would be something like -
{
"Desc": "Hello",
"City" : "City1",
"Age": "21"
}
{
"Description" : "World",
"City" : "City2",
"Age": "25"
}
I'm using copy command to pull the data from the JSON above into my table in redshift. The table has a column named "description_data". This would store values of either "Desc" or "Description". So I want my path file to identify using an "OR" condition.
This is the path file that I'm currently using -
{
"jsonpaths": [
"$['Desc']",
"$['City']",
"$['Age']"
]
}
Which is working fine.
What I'm trying to do is the below (this is where I'm unsure if there is any syntax or functionality to achieve the objective)
{
"jsonpaths": [
"$['Desc']" or "$['Description']",
"$['City']",
"$['Age']"
]
}
No, Redshift doesn't support this.
You can issue two copy commands, one with Desc, and another with Description, to load the data into two temporary tables. After that, you can merge the two into your final table.

How to display 'c' array values alone from the given JSON document below using MongoDB?

I am a newbie to MongoDB. I am experimenting the various ways of extracting fields from a document inside collection.
Here in the below JSON document, I am finding it difficult to get extract it according to my need
{
"_id":1,
"dependencies":{
"a":[
"hello",
"hi"
],
"b":[
"Hmmm"
],
"c":[
"Vanilla",
"Strawberry",
"Pista"
],
"d":[
"Carrot",
"Cauliflower",
"Potato",
"Cabbage"
]
},
"productid":"25",
"date":"Thu Jul 30 11:36:49 PDT 2015"
}
I need to display the following output:
c:[
"Vanilla",
"Strawberry",
"Pista"
]
Can anyone please help me in solving it?
MongoDB Aggregation comes into rescue to get the result you are looking for :
$Project--> Passes along the documents with only the specified fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.
db.collection.aggregate( [
{ $project :
{ c: "$dependencies.c", _id : 0 }
}
]).pretty();
As per the output you required, we just need to project ( display) the field "dependencies.c" , so we are creating a new field "c" and assigining the value of the "dependencies.c" into it.
Also by defalut "_id" field will be display along with the result, since you dont need it, so we are suppressing of the _id field by assigining "_id" : <0 or false>, so that it will not display the _id field in the output.
The above query will fetch you the result as below :
"c" : [
"Vanilla",
"Strawberry",
"Pista"
]

Referencing JSON elements in AppleScript

I have a JSON result I am trying to work with in AppleScript, but because the top level items are "unnamed" I can only access them by piping the item reference, which in this case is a number. As a result, I can't iterate through it, it has to be hard coded (scroll down to the last code sample to see what I mean)
For example, this is the JSON I'm looking at:
{
"1": {
"name": "Tri 1"
},
"2": {
"name": "Tri 2"
},
"3": {
"name": "Tri 3"
},
"4": {
"name": "Orb Dave"
},
"5": {
"name": "Orb Fah"
}
}
With the help of JSON Helper I get the JSON to a more usable format (for AppleScript).
{|3|:{|name|:"Tri 3"}, |1|:{|name|:"Tri 1"}, |4|:{|name|:"Orb Dave"}, |2|:{|name|:"Tri 2"}, |5|:{|name|:"Orb Fah"}}
I can then use this code to get a list of "lights" the objects in question:
set lights to (every item in theReturn) as list
repeat with n from 1 to count of lights
set light to item n of lights
log n & light
end repeat
From that, I get:
(*1, Tri 3*)
(*2, Tri 1*)
(*3, Orb Dave*)
(*4, Tri 2*)
(*5, Orb Fah*)
You may notice the result is not in the desired order. The index is the index within the list of lights. It's not the number that appears at the top of the object. If you look to the top two pre-formated areas, you'll see the items 1,2 and 3 are Tri 1, Tri 2, and Tri 3. It is correct that Tri 3 comes first, Tri 1 second, and an Orb is third.
What I need to do is find a way to be able to iterate through the JSON in any order (sorted or not) and be able to line up "1" with "Tri 1", "3" with "Tri 3" and "5" with "Orb Fah". But I can't find ANY way to interact with the returned JSON that lets me reference the third light and return it's name. The ONLY way I can seem to be able to do it is to hard code the light indexes, such that:
log |name| of |1| of theReturn
log |name| of |2| of theReturn
log |name| of |3| of theReturn
log |name| of |4| of theReturn
log |name| of |5| of theReturn
which gives me the correct light with the correct name:
(*Tri 1*)
(*Tri 2*)
(*Tri 3*)
(*Orb Dave*)
(*Orb Fah*)
I'm thinking the problem is arising because the light ID doesn't have a descriptor or sorts. That I can't change, but I need to iterate through them programatically. Hard coding them as above is not acceptable.
Any help would be appreciated
You are dealing with a list of records here, not a list of lists. Records are key/value pairs. They do not have indexes like a list. That makes it easy if you know the keys because you just ask for the one you want. And your records have records inside them so you have 2 layers of records. Therefore if you want the value of the |name| record corresponding to |3| record then ask for it as you've discovered...
set jsonRecord to {|3|:{|name|:"Tri 3"}, |1|:{|name|:"Tri 1"}, |4|:{|name|:"Orb Dave"}, |2|:{|name|:"Tri 2"}, |5|:{|name|:"Orb Fah"}}
set record3name to |name| of |3| of jsonRecord
The downside of records in applescript is that there is no command to find the record keys. Other programming languages give you the tools to find the keys (like objective-c) but applescript does not. You have to know them ahead of time and use them as I showed.
If you don't know the keys ahead of time then you can either use JSON Helper to give you the results in a different form or use a different programming language (python, ruby, etc) to extract the information from the records.
One other option you have is to just use the json text itself without using JSON Helper. For example, if you have the json as text then you can extract the information using standard applescript commands for text objects. Your json text has the information you want on the 3rd line, the 6th, 9th etc. You could use that to your advantage and do something like this...
set jsonText to "{
\"1\": {
\"name\": \"Tri 1\"
},
\"2\": {
\"name\": \"Tri 2\"
},
\"3\": {
\"name\": \"Tri 3\"
},
\"4\": {
\"name\": \"Orb Dave\"
},
\"5\": {
\"name\": \"Orb Fah\"
}
}"
set jsonList to paragraphs of jsonText
set namesList to {}
set AppleScript's text item delimiters to ": \""
repeat with i from 3 to count of jsonList by 3
set theseItems to text items of (item i of jsonList)
set end of namesList to text 1 through -2 of (item 2 of theseItems)
end repeat
set AppleScript's text item delimiters to ""
return namesList
For each index, loop through all the items in the list looking for the one whose name matches the index:
tell application "System Events"
-- Convert the JSON file to a property list using plutil.
do shell script "plutil -convert xml1 /Users/mxn/Desktop/tri.json -o /Users/mxn/Desktop/tri.plist"
-- Read in the plist
set theItems to every property list item of property list file "/Users/mxn/Desktop/tri.plist"
set theLights to {}
-- Iterate once per item in the plist.
repeat with i from 1 to count of theItems
set theName to i as text
-- Find the item whose name is the current index.
repeat with theItem in theItems
if theItem's name is theName then
-- We found it, so add it to the results.
set theValue to theItem's value
copy {i, theValue's |name|} to the end of theLights
-- Move on to the next index.
exit repeat
end if
end repeat
end repeat
return theLights
end tell
Result:
{{1, "Tri 1"}, {2, "Tri 2"}, {3, "Tri 3"}, {4, "Orb Dave"}, {5, "Orb Fah"}}
Ideally, instead of the nested loop, we’d be able to say something like this:
set theName to i as text
set theItem to (the first item in theItems whose name is theName)
But unfortunately that produces an error.
This solution also demonstrates an alternative to JSON Helper: you can convert the JSON file to a property list using the handy plutil command line tool and use System Events' built-in support for property lists.