Insert a new dynamic record in JSON under array field

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

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

Push Documentation

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

1 Like

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

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
}

@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 When including JSON or code in a post to this forum, please use the “Preformatted text” feature of this editor. It’s the 6th icon from the left, and looks like this: </>

This greatly enhances readability, and also allows the content to be copy/pasted in a usable way. If you don’t use the feature on JSON, the editor will modify the double quote characters so that the resulting data isn’t usable if it’s copy/pasted. It also loses all the indentation, which makes it very hard to read.

Thanks.

Hey thanks Taylor. i done the modification and indeed it helps in readability.

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:

and here is the array after the expression:

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.


the expected Output is as follows:

[
{
"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"
}
]
},
},
},
}
]

As I said in my previous reply, enable Pass through in the Mapper. You’ll get the following result:

  {
    "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"
            }
          ]
        }
      }
    }
  }
]

Thanks Boston , its working Now.

1 Like