cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Insert a new dynamic record in JSON under array field

KTsnap
New Contributor III
Hi Everybody,
Kindly help.
Am trying to insert a dynamic value under the **value** array using extend function but unable to do so. 
[
{
"entity":
{
"attributes":
{
"type":"CustomField","url":"A"
}
"Metadata":
{
"valueSet":
{
"value":
[
{
"color":null,"default":false,"description":null,"isActive":null,"label":"None","urls":null,"valueName":"None"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Country","urls":null,"valueName":"Country"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Employee","urls":null,"valueName":"Employee"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Both","urls":null,"valueName":"Both"
}
]
},
},
},
},
{
"src_code":"Active","tgt_code":null
}
]







Output:

[
{
"entity":
{
"attributes":
{
"type":"CustomField","url":"A"
}
"Metadata":
{
"valueSet":
{
"value":
[
{
"color":null,"default":false,"description":null,"isActive":null,"label":"None","urls":null,"valueName":"None"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Country","urls":null,"valueName":"Country"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Employee","urls":null,"valueName":"Employee"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Both","urls":null,"valueName":"Both"
},
{
"color":null,"default":false,"description":null,"isActive":null,"label":"Active","urls":null,"valueName":"Active"
}
]
},
},
},
}
]Preformatted text
14 REPLIES 14

bojanvelevski
Valued Contributor

Hi @KTsnap,

There is multiple ways of adding an element to an array. Besides the .push() method, you can use array concatenation:

$value.concat([{"color":null,"default":false,"description":null,"isActive":null,"label":"Active","urls":null,"valueName":"Active"}])

OR

[...$value,{"color":null,"default":false,"description":null,"isActive":null,"label":"Active","urls":null,"valueName":"Active"}]

Regards,
Bojan

KTsnap
New Contributor III

HI Bojan,

But it can be dynamic depending on the number of inputs.
its unsure in every run , whether we need to add one element or more than one element to the existing value. How we can implement the same for dynamic values

@KTsnap, Concatenating can be on multiple elements. In the square brackets, list the elements that need to be added to the original array. Thatโ€™s basically joining 2 arrays (array1 which is original, array2 which is array of values that need to be added)

KTsnap
New Contributor III

Hi Bojan,
I tried the above syntax. But its giving only the value inside the array. I need those value along with the input (the way extend works)

Hey @KTsnap ,

Here is the array before the expression:

image

and here is the array after the expression:

image

Re-check if you are doing something wrong. If you were referring to the complete payload, than make sure you check Pass Through in the Mapper.