10-01-2021 04:14 AM
Hi Team,
I have below input
[
{
"parents": "A",
"value": "1"
},
{
"parents": "B",
"value": "2"
},
{
"parents": "C",
"value": "3"
},
{
"parents": "D",
"value": "4"
}
]
When i tried using the expression :
{}.extend({“controllingFieldValue”:[$parents],“valueName”:$value})
I got the below Output
[
{
"controllingFieldValue": [
"A"
],
"valueName": "1"
},
{
"controllingFieldValue": [
"B"
],
"valueName": "2"
},
{
"controllingFieldValue": [
"C"
],
"valueName": "3"
},
{
"controllingFieldValue": [
"D"
],
"valueName": "4"
}
]
But i am expecting the above output to be in one JSON.Can experts please guide.
10-01-2021 06:20 AM
Hi @KTsnap,
Don’t know what you mean by “JSON” output? If you are referring to the Array value of “controllingFieldValue”, than just remove the square brackets from your expression
10-01-2021 06:28 AM
Hi Bojan,
IN this way ,
[
"Value" : {
{
"controllingFieldValue": [
"A"
],
"valueName": "1"
},
{
"controllingFieldValue": [
"B"
],
"valueName": "2"
},
{
"controllingFieldValue": [
"C"
],
"valueName": "3"
},
{
"controllingFieldValue": [
"D"
],
"valueName": "4"
}
}
]
10-01-2021 06:38 AM
This is not a valid JSON. The value of the “Value” object can either be a string,number,boolean, array , or object with key value pairs, but not multiple object literals. You can group all of the documents in one array ($group) , and use the following expression:
$group.map(x=>{"controllingFieldValue":[x.parents],"valueName":x.value})
And map the target path as Value, than you’ll have all of the objects but in an array.
10-01-2021 08:38 AM
This helped… Thank you 🙂