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

viktor_n
Contributor II

Hi @KTsnap,

If I understood well, you want to add another element to the array, and fields which will be in the new element will be populated from the object which is bellow. Am I right ?

{
    "src_code": "Active",
    "tgt_code": null
}

KTsnap
New Contributor III
Hi Viktor,
I want to insert new record in the below given array .If suppose there are 4 records under the value array. I want to add the 5th record in the array of value if the another input is  { "src_code" : "Active"}

“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
}

expected Output 
“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”
}
]


Please let me know if it is unclear.

type or paste code here

@KTsnap

I think you may be looking for the Array method push:

Screen Shot 2021-09-20 at 9.26.16 AM

Push Documentation

KTsnap
New Contributor III

Yes Viktor. and that object needs to be removed in the output. and the number of input object would be dynamic.
{
“src_code”: “Active”,
“tgt_code”: null
}