Skip to main content
Question

How can I add an element to a JSON array using the JSON Object Manager

  • April 4, 2022
  • 3 replies
  • 2145 views

Forum|alt.badge.img+6

Hi

 

I use the JSON Object Manager regularly for retrieving or setting values from a JSON string.

But how can I actually add elements to an array?

For example, I have a JSON string like {"employer":"Joe","roles":[]} and I would like to add roles so the result should look like {"employer":"Joe","roles":[{"roleID":"developer"},{"roleID":"analyst"}]}

 

Any ideas how to do that with JSON Object Manager Package?

3 replies

Forum|alt.badge.img+6
  • Author
  • Navigator | Tier 3
  • April 7, 2022

Hi @MICAH SMITH​ 

Since you recorded a quickstep using the JSON Object Manager, I was hoping that you could shine your light on my question?

Is would like to know if it is actually possible to create or extend a JSON object using the JSON Object Manager?

Thanks in advance,

 

Nordine


Micah.Smith
Automation Anywhere Team
Forum|alt.badge.img+14
  • Automation Anywhere Team
  • April 7, 2022

I dont think you can do it in the JSON Object Manager package...I believe you can only set values there, not actually adding elements...I think the easiest way to do it in Automation 360 would be to have a small JavaScript function that could do it.

 

var jsonStr = '{"employer":"Joe","roles":[]}';

var obj = JSON.parse(jsonStr)

obj['roles'].push({"role":"developer"});

jsonStr = JSON.stringify(obj); 

console.log(jsonStr);

 

you'd just need to make that a function and pass in your JSON as a string in a list as well as the target object add as a string of the same list.

 


Forum|alt.badge.img+6
  • Author
  • Navigator | Tier 3
  • April 8, 2022

Thanks for your reply. I think that your suggestion to use JavaScript is at least a more elegant solution than doing string manipulations.