how to get objects which param contains substring in value using JSONata? - jsonata

here is data:
[
{name:"Hello"},
{name:"World"},
{name:"Hello World"}
]
how to build proper JSONata query to get all entries where name contains World?
I did try the "'World' in name", but it returns undefined
Thanks.

Use the $contains() function within a filter expression ($[...] filters the input array):
$[$contains(name, "World")]
See this in the JSONata Exerciser: http://try.jsonata.org/BJDPGXzEG

Related

How to use jsonpath length() function if list of elements is nameless?

So usualy I use this function like this $.some_element.length()
But now my json has no name for the list of elements it has this structure:
[
{somedata1},
{somedata2}
]
How can I found out length of this list? I've tried $.length() and $.*.length() nothing works.

Powerautomate Parsing JSON Array

I've seen the JSON array questions here and I'm still a little lost, so could use some extra help.
Here's the setup:
My Flow calls a sproc on my DB and that sproc returns this JSON:
{
"ResultSets": {
"Table1": [
{
"OrderID": 9518338,
"BasketID": 9518338,
"RefID": 65178176,
"SiteConfigID": 237
}
]
},
"OutputParameters": {}
}
Then I use a PARSE JSON action to get what looks like the same result, but now I'm told it's parsed and I can call variables.
Issue is when I try to call just, say, SiteConfigID, I get "The output you selected is inside a collection and needs to be looped over to be accessed. This action cannot be inside a foreach."
After some research, I know what's going on here. Table1 is an Array, and I need to tell PowerAutomate to just grab the first record of that array so it knows it's working with just a record instead of a full array. Fair enough. So I spin up a "Return Values to Virtual Power Agents" action just to see my output. I know I'm supposed to use a 'first' expression or a 'get [0] from array expression here, but I can't seem to make them work. Below are what I've tried and the errors I get:
Tried:
first(body('Parse-Sproc')?['Table1/SiteConfigID'])
Got: InvalidTemplate. Unable to process template language expressions in action 'Return_value(s)_to_Power_Virtual_Agents' inputs at line '0' and column '0': 'The template language function 'first' expects its parameter be an array or a string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#first for usage details.'.
Also Tried:
body('Parse-Sproc')?['Table1/SiteconfigID']
which just returns a null valued variable
Finally I tried
outputs('Parse-Sproc')?['Table1']?['value'][0]?['SiteConfigID']
Which STILL gives me a null-valued variable. It's the worst.
In that last expression, I also switched the variable type in the return to pva action to a string instead of a number, no dice.
Also, changed 'outputs' in that expression for 'body' .. also no dice
Here is a screenie of the setup:
To be clear: the end result i'm looking for is for the system to just return "SiteConfigID" as a string or an int so that I can pipe that into a virtual agent.
I believe this is what you need as an expression ...
body('Parse-Sproc')?['ResultSets']['Table1'][0]?['SiteConfigID']
You can see I'm just traversing down to the object and through the array to get the value.
Naturally, I don't have your exact flow but if I use your JSON and load it up into Parse JSON step to get the schema, I am able to get the result. I do get a different schema to you though so will be interesting to see if it directly translates.

Return True if Array Contains a Specific String - JSONata

Is there a way in JSONata to have a function return TRUE if it finds a specific string within a provided array? For example I have an array of colors:
const myArray = [red,blue,green,pink]
I am trying to figure out an expression that would search that array for "blue" and return true if it finds the value.
On the JSONata documentation, I found a function called $boolean(arg)that I think I would need to use but I'm not sure how to implement it. The documentation shows an argument type option as "array: contains a member that casts to true", but I can't really tell how to implement it.
Would it be as simple as $boolean(myArray, "blue")?
The in operator is what you need. See https://docs.jsonata.org/comparison-operators#in-inclusion
In your case, the expression "blue" in myArray will return true. See https://try.jsonata.org/r0q7GnSOh
edit: Thought this was python, but maybe you could use something similar for JSONata
you can make a for loop with an if condition to check your condition
listOfStrings = ['red','green','blue']
for strings in listOfStrings:
if listOfStrings[strings] == 'blue':
return True

Why can't I append an object to an array in rethinkdb?

I am trying to append an object into an array in rethink. Here is how I am trying to append it:
rethink.table('shifts')
.get(shiftId)
.update(row => row("milestones").default([]).append({
dateAchieved: "2017-01-01",
phaseType: "TEST"
})).run(rethinkConnection)
The error I get is this:
first_error: 'Inserted value must be an OBJECT (got ARRAY):\n[\n\t{\n\t\t"dateAchieved":\t"2017-01-01",\n\t\t"phaseType":\t"TEST"\n\t}\n]',
I also tried the same code about, but removed .default([]), which did not change anything.
I have tried logging out what row('milestones') is and this is the result:
var_15("milestone")
I was expecting to see an array. What else is necessary to add an object to an array? How do I make this work?
You need to update document with a document not an array. Your reql should look like this
rethink.table('shifts')
.get(shiftId)
.update({
milestones: rethink.row('milestones').append({
dateAchieved: "2017-01-01",
phaseType: "TEST"
})
})
.run(rethinkConnection)
this is because the .append command does not commit data to the database it only returns an in memory modified array

How do I extract a value from this Ruby hash?

I'm using the Foursquare API, and I want to extract the "id" value from this hash
[{"id"=>"4fe89779e4b09fd3748d3c5a", "name"=>"Hitcrowd", "contact"=>{"phone"=>"8662012805", "formattedPhone"=>"(866) 201-2805", "twitter"=>"hitcrowd"}, "location"=>{"address"=>"1275 Glenlivet Drive", "crossStreet"=>"Route 100", "lat"=>40.59089895083072, "lng"=>-75.6291255071468, "postalCode"=>"18106", "city"=>"Allentown", "state"=>"Pa", "country"=>"United States", "cc"=>"US"}, "categories"=>[{"id"=>"4bf58dd8d48988d125941735", "name"=>"Tech Startup", "pluralName"=>"Tech Startups", "shortName"=>"Tech Startup", "icon"=>"https://foursquare.com/img/categories/shops/technology.png", "parents"=>["Professional & Other Places", "Offices"], "primary"=>true}], "verified"=>true, "stats"=>{"checkinsCount"=>86, "usersCount"=>4, "tipCount"=>0}, "url"=>"http://www.hitcrowd.com", "likes"=>{"count"=>0, "groups"=>[]}, "beenHere"=>{"count"=>0}, "storeId"=>""}]
When I try to extract it by using ['id'], I get this error can't convert Symbol into Integer. How do I extract the value using ruby? Also, how do I do this for multiple hashes extracting the "id" value each time?
Please pardon my inexperience. Thanks!
It's wrapped in an array, that's what the [ and ] mean on the start and end. But it also looks like this array only one object in it, which is the hash you really want.
So assuming you want the first object in this array:
mydata[0]['id'] # or mydata.first['id'] as Factor Mystic suggests
But usually when an API returns an Array there is a reason (it might return many results instead of just one), and naively plucking the first item from it my not be what you want. So be sure you are getting the kind of data you really expect before hard coding this into your application.
For multiple hashes, if you want to do something with the id (run a procedure of some kind) then
resultsArray.each do |person|
id = person["id"] #then do something with the id
end
If you want to just get an array containing the ids then
resultsArray.map{|person| person["id"]}
# ["4fe89779e4b09fd3748d3c5a", "5df890079e4b09fd3748d3c5a"]
To just grab the one item from the array, see Alex Wayne's answer
To get an array of ids, try: resultsArray.map { |result| result["id"] }

Resources