I have a json payload in Mule that I am trying to convert to xml. Whatever value is in "entity_id" should be the "payment_id" in my output. Also whatever value comes for the key "selected_payment_option" should be "method" in my output.
Please see my sample input here:
{
"items": [
{
"payment": {
"entity_id": 1485222,
"method": "m2_kp"
},
"extension_attributes": {
"payment_addition_info":[
{
"key": "m1_code",
"value": "over_time"
},
{
"key": "order_id",
"value": "4f86-2cce-4870-ad05-089a333"
},
{
"key": "selected_payment_option",
"value": "slice_by_card"
}
]
}
}
]
}
And my desired output for this would be
<root>
<payment>
<payment_id>1485222</payment_id>
<method>slice_by_card</method>
</payment>
</root>
Any help would be greatly appreciated. Thank you!
Used Map since items is an array hence there can be multiple entities inside items.
Used default -> "" if key does not match selected_payment_option
DW
%dw 2.0
output application/xml
---
root:payment:payload.items map {
payment_id: $.payment.entity_id,
method: (flatten($..payment_addition_info) filter ($.key=="selected_payment_option"))[0].value default ""
}
Related
I created a simple logic app triggered by sending a json payload and I need to retrieve a value of a property in order to use it in a condition scope.
Thanks for you help
Please use Parse Json action, please click Use sample payload to generate schema, and then paste your json to generate the schema needed to parse the json:
Then select the key of the value you want to get
===============update=============================
I defined a json string as input.
According to the json string you provided, the generated schema should look like this:
{
"properties": {
"CusttId": {
"type": "string"
},
"Direction": {
"type": "string"
},
"MyId": {
"type": "string"
},
"Reason": {
"type": "string"
},
"TestDateTime": {
"type": "string"
}
},
"type": "object"
}
I did a test and it can retrieve MyId:
response= [
{
"id": "123",
"name: "user1",
"location": "USA"
},
{
"id": "133",
"name: "user2",
"location": "CANADA"
},
{
"id": "",
"name": "user3",
"location": "INDIA"
}
]
I am trying to validate the above json array using the schema and want to validate that values of my keys should not be empty or blank.
I am using the below code:
* def schema = {"id": "#notnull", "name": "#notnull", "location": "#notnull"}
* match each response contains schema
But even though my response has "id":"" -- id is empty/blank in the 3rd index in json array. Still I get a pass in my scenario.
Is this the correct way or is there any other way to handle empty values in schema for a json array response.
Your help would be appreciated.
Thank you
Ideally we should have #notblank but we don't because it is so rare.
Do this instead:
* match each response contains { id: "#? _ != ''" }
I have some restmethods with powershell (VMWare vRA API) where I get a template for a development request and then I need to fill that (JSON format).
The "data" part of the template has different properties like:
Name : Test
selectedNetworks :
selectedServices :
My question:
How do I get my array $networks (network1,network2,network3) to fill the json element "selectedNetworks" that it becomes like this:
"key": "selectedNetworks",
"value": {
"type": "multiple",
"elementTypeId": "STRING",
"items": [
{
"type": "string",
"value": "network1"
},
{
"type": "string",
"value": "network2"
},
{
"type": "string",
"value": "network3"
}
]
}
I know how to add in "simple" values like the name is
$WebRequestBodyData.Name="$Name"
But how do I get the complex format above into $WebRequestBodyData.selectedNetworks?
Any help is greatly appreciated!
Thanks and best regards,
Ville
I could solve it like that:
$networkjson = '{"type": "multiple", "elementTypeId": "STRING", "items": [] }' | ConvertFrom-Json
foreach($nwitem in $networks){
$networkjson.items += $nwitem
}
$BodyData.selectedNetworks += $networkjson
I m working with mongodb and restheart.
In my nosql db i have a unique document with this structure:
{
"_id": "docID",
"users": [
{
"userID": "12",
"elements": [
{
"elementID": "1492446877599",
"events": [
{
"event1": "one"
},
{
"event2": "two",
}
]
}
},
{
"userID": "11",
"elements": [
{
"elementID": "14924",
"events": [
{
"event1": "one"
},
{
"event2": "two",
}
]
}
}
]
}
how can i build an url-query in order to get the user with id 11?
Using mongo shell it should be something like this one:
db.getCollection('collection').find({},{'users':{'$elemMatch':{'userID':'12'}}}).pretty()
I cannot find anything similar on restheart.
Could someone help me?
Using this
http://myHost:port/documents/docID?filter={%27users%27:{%27$elemMatch%27:{%27userID%27:%2712%27}}}
restheart returns me all the documents: userID 11 and 12.
Your request is against a document resource, i.e. the URL is http://myHost:port/documents/docID
The filter query parameter applies for collection requests, i.e. URLs such as http://myHost:port/documents
In any case you need to projection (the keys query parameter) to limit the returned properties.
You should achieve it with the following request (I haven't tried it) using the $elementMatch projection operator:
http://myHost:port/documents?keys={"users":{"$elemMatch":{"userID":"12"}}}
In my data, I have two fields that I want to use as an index together. They are sensorid (any string) and timestamp (yyyy-mm-dd hh:mm:ss).
So I made an index for these two using the Cloudant index generator. This was created successfully and it appears as a design document.
{
"index": {
"fields": [
{
"name": "sensorid",
"type": "string"
},
{
"name": "timestamp",
"type": "string"
}
]
},
"type": "text"
}
However, when I try to make the following query to find all documents with a timestamp newer than some value, I am told there is no index available for the selector:
{
"selector": {
"timestamp": {
"$gt": "2015-10-13 16:00:00"
}
},
"fields": [
"_id",
"_rev"
],
"sort": [
{
"_id": "asc"
}
]
}
What have I done wrong?
It seems to me like cloudant query only allows sorting on fields that are part of the selector.
Therefore your selector should include the _id field and look like:
"selector":{
"_id":{
"$gt":0
},
"timestamp":{
"$gt":"2015-10-13 16:00:00"
}
}
I hope this works for you!