Splitting Values in Dataweave Mule 4 - transform

I am trying to split input values using Regex.
My input will be like this
"salesforce.com/jobs/2020/06-2020"
the expression am using is below
%dw 2.0
output application/json
---
"FName": vars.Folderstruct splitBy(/[\/]/)
Output:
{
"FName": [
"salesforce.com",
"jobs",
"2020",
"06-2020"
]
}
But i need my output like this
{
"FName":"salesforce.com"
},
{
"FName":"jobs"
},
{
"FName":"2020"
},
{
"FName":"06-2020"
}
How can i achieve this through transform?

Once you have the list with values, what you need is to create a new object with each one, and for that, you can use the map function like this:
%dw 2.0
output application/json
---
vars.Folderstruct splitBy(/[\/]/) map ((item, index) ->
{
FName: item
}
)

Related

Fetch key value from json data using terraform

I have a json data file from which I need to fetch only the id attribute value into a separate list variable. I tried for loop but not able to get the required data using terraform. Can someone tell how to fetch the id part ? your help is much appreacited.
code:
locals {
data = jsondecode(file("./data.json"))[*]
sections = [ for item in local.data : item ]
}
output "ids" {
value = [for a in local.sections[0]: a]
}
json file:
{
"c":[
{
"id":"6",
"key":"c",
"name":"s01"
}
],
"l":{
"id":"7",
"key":"l",
"name":"s02"
},
"m":{
"id":"8",
"key":"mp",
"name":"s03"
},
"n":{
"id":"5",
"key":"cn",
"name":"s04"
},
"od":"odk",
"s":{
"id":"9",
"key":"cs",
"name":"s05"
},
"ss":{
"id":"1",
"key":"ss",
"name":"s06"
},
"in":{
"id":"65",
"key":"cn",
"name":"s07"
},
"b":{
"id":"2",
"key":"cb",
"name":"s08"
}
}
Not sure if "od":"odk", is mistake or not, but you can do the following:
locals {
data = jsondecode(file("./data.json"))
}
output "ids" {
value = [for v in values(local.data): v.id if can(v.id)]
}

Get Values from a Dictionary in Python

I have a Database query returning objects as a list like this
table_query_response:
{
"Items":[
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2f4f826"
},
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2sfdgd"
}
],
"Count":2,
"ScannedCount":2,
"ResponseMetadata":{
"RequestId":"UEIQVN6HFB9K9Q20IE6J8L48CRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"HTTPStatusCode":200,
"HTTPHeaders":{
"server":"Server",
"date":"Thu, 10 Mar 2022 13:42:17 GMT",
"content-type":"application/x-amz-json-1.0",
"content-length":"151",
"connection":"keep-alive",
"x-amzn-requestid":"UEIQVN6HFB9K9Q20IE6J8L48CRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"x-amz-crc32":"2870685196"
},
"RetryAttempts":0
}
}
and a different query to just get the specific items
group_query:
[
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2f4f826"
},
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2sfdgd"
}
]
I want to get a list with values for the key groupid and store it like (6501e5ac-59b2-4d05-810a-ee63d2f4f826, 6501e5ac-59b2-4d05-810a-ee63d2sfdgd).
There can be more than two entries in that group query so I will need to get all the values for the groupid.
[x['groupId'] for x in table_query_response['Items']]

How do I make a field path include a value in same document using MongoDB/pyMongo?

The documents in the MongoDB I am querying can be stripped down to this as an example:
{
"date":"2019-08-15",
"status":"5345",
"foo":
{
"bar":
{
"years":
{
"2018":
{
"const":1234
},
"2019":
{
"const":4321
}
}
}
}
}
I am trying to get the "const" values from this document using pyMongo.
The keys in "years" is varying with the "date" of the document.
I have attempted to use this pipeline where I try to use the year of "date" to get the "const" from this year:
pipeline=[
{'$match':{'status':{'$exists': True}}},
{'$project':
'const_thisYear':{
'$let':{
'vars':{
'yr':{ '$year': {'$convert':{'input': '$date','to': 'date'}}},
'res': '$foo.bar.years'
},
'in': '$$res.$$yr.const'
}
}
}
]
When aggregating I get the following python exception:
OperationFailure: FieldPath field names may not start with '$'.
How do I do this correctly?
Python 3.7.7
You should revise your collection structure to not store data as keys; but irrespective, just using regular python dict manipulation can get you out of the hole:
for doc in db.mycollection.find({'status': {'$exists': True}}, {'foo.bar.years': 1}):
for year, year_value in doc['foo']['bar']['years'].items():
print(year, year_value.get('const'))

Extract a list of profileId's using jsonpath

I have the following JSON object r2,
[
{
"reserva_id":"200",
"estancias":[
{
"reserva_estancia":"266",
"huespedes":[
{
"reserva_huesped":"272",
"reserva_estancia":"266",
"numero_huesped":"1",
"huesped":"123",
"huesped_nombre":"dos dos, dos",
"rfid":null
},
{
"reserva_huesped":"276",
"reserva_estancia":"266",
"numero_huesped":"2",
"huesped":"183",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
}
]
}
]
},
{
"reserva_id":"201",
"estancias":[
{
"huespedes":[
{
"reserva_huesped":"273",
"reserva_estancia":"267",
"numero_huesped":"1",
"huesped":"148",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
},
{
"reserva_huesped":"277",
"reserva_estancia":"267",
"numero_huesped":"2",
"huesped":"187",
"huesped_nombre":"TEST TEST, TESTCIVITFUN",
"rfid":null
}
]
}
]
}
]
I am trying to get the first huesped for each reservation, and for that I am using the following script, to create a list called profiles and store profileId's:
def profiles = jsonpath(r2,'$..[:].estancias[:].huespedes[0].huesped')
The output should be the following:
[
"123",
"148"
]
However, when I print profiles.text I get all the content of the estancias object, instead of just the huesped number.
When using Jayway's JSONPath like this I get the desired oputput:
$..[*].estancias[*].huespedes[0].huesped
You can try the path expression with your JSON here online.

AQL filter by array of IDs

If I need to filter by array of IDs, how would I do that using bindings?
Documentation does not provide any hints on that.
for c in commit
filter c.hash in ['b0a3', '9f0eb', 'f037a0']
return c
Updating the answer to deal with bindings reference that I missed.
LET commit = [
{ name: "111", hash: "b0a3" },
{ name: "222", hash: "9f0eb" },
{ name: "333", hash: "asdf" },
{ name: "444", hash: "qwer" },
{ name: "555", hash: "f037a0" }
]
FOR c IN commit
FILTER c.hash IN #hashes
RETURN c
The key is that when you send the bind param #hashes, it needs to be an array, not a string that contains an array.
If you use the AQL Query tool via the ArangoDB Admin Tool, make sure you click the "JSON" button in the top right to ensure the parameter hashes has the value
["b0a3", "9f0eb", "f037a0"] and not
"['b0a3', '9f0eb', 'f037a0']"
If you want to send a string as a parameter such as "b0a3","9f0eb","f037a0", so { "hashes": "\"b0a3\",\"9f0eb\",\"f037a0\"" } as bind parameter, then you can split the string into an array like this:
LET commit = [
{ name: "111", hash: "b0a3" },
{ name: "222", hash: "9f0eb" },
{ name: "333", hash: "asdf" },
{ name: "444", hash: "qwer" },
{ name: "555", hash: "f037a0" }
]
FOR c IN commit
FILTER c.hash IN REMOVE_VALUE(SPLIT(#hashes, ['","', '"']), "")
RETURN c
This example will take the string #hashes and then SPLIT the contents using "," and " as delimiters. This converts the input variable into an array and then the query works as expected. It will also hit an index on the hash attribute.
The delimiters are enclosed with single quote marks to avoid escaping, which would also be possible but less readable: ["\",\"", "\""]
Note that "," is listed first as delimiter, so that the result of the SPLIT is
[ "", "9f0eb", "b0a3", "f037a0" ] instead of
[ "", ",", "9f0eb", "b0a3", "f037a0" ].
The empty string element caused by the first double quote mark in the bind parameter value, which would make the query return commit records with an empty string as hash, can be eliminated with REMOVE_VALUE.
The recommended way is to pass ["b0a3", "9f0eb", "f037a0"] as array however, as shown at the beginning.
like this:
with person FOR id in ["person/4201061993070840084011","person/1001230840198901011999","person/4201008406196506156918"]
FOR v,e,p In 1..1 ANY id
relation_samefamily,stay,relation_internetbar,relation_flight,relation_train
OPTIONS {
bfs:true
}
FILTER (p.edges[*]._from ALL IN ["person/42010619930708400840084011","person/10012310840989084001011999","person/4201060840196506156918"] and p.edges[*]._to ALL IN ["person/4201061993070808404011","person/1001231908408901011999","person/4200840106196506156918"])
RETURN {v,e}

Resources