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
}