Manipulating Json Array - adding/removing info

Hello I have a complex JArray which i want to remove an array and add an object. I have spent 2 days googling trying to learn how to do it, but it seems much more difficult than handling a xml file.
My Json looks like this:

[
{
“wfTaskActions”: [
{
“description”: “Godkjenne”,
“wfTaskAction”: “AP”,
“requiresComment”: false
},
{
“description”: “Avvise”,
“wfTaskAction”: “RJ”,
“requiresComment”: true
}
],
“isParked”: false,
“voucherNo”: 900061930,
“sequenceNo”: 1,

Deserialized to JsonArray it looks like this:
JArray(2)
{
JObject(21)
{
JProperty(1)
{
JArray(2)
{
JObject(3)
{
JProperty(1)
{
[Godkjenne]
},
JProperty(1)
{
[AP]
},
JProperty(1)
{
[False]
}
},
JObject(3)
{
JProperty(1)
{
[Avvise]
},
JProperty(1)
{
[RJ]
},
JProperty(1)
{
[True]
}
}
}
},
JProperty(1)
{
[False]
},
JProperty(1)
{
[900061930]

The Json cointaing 2 main Arrays (2 accounting rows on an invoice)

  1. I want to remove the first JObject “wfTaskActions” containing the 2 arrays inside, in both main arrays

  2. I want to add a JObject “wfTaskAction”:“AP”, before the JObject “isParked”: false, in both main arrays

How can i achive this?

may we ask you to share the JSON as Text file or at least user the </> Format from editor and share with us the output from YourJObjectVar.toString

Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

It would also help us when you share with us a sample of the expected final result. Thanks for support

Also have a look at the below simplified sample and some statements

grafik


before:
grafik

Using invoke Code / invoke Method for calling sub (subroutines, void methods, methods without a return value and not usable within an assign activity)

grafik
grafik

Result:
grafik

A quick dirty line also could look like this (used within Invoke Code)

io_JArray(0).Value(Of JObject).Property("wfTaskActions").Replace(new JProperty("wfTaskAction","AP")

Hello. This was exactly what I was looking for, thank you.

Also thanks for the tip about using the “Immediate” panel during debugging, never used that before, but I see now it has a very high usecase :slight_smile:

Another question:
My invoices can contain multiple accounting rows (unknown number) The number of “main arrays” in the JArray can vary. Therefor the Remove and Add lines of code need to be done for each accounting row (array). My solution was assigning index = JArray.Count and then doing a while loop with condition index > 0. And then assigning a new idx = index-1 to since array indexes start at zero (0).

Maybe there is another agile way for invoking the codes for all the arrays the JArray might have ?

Let’s get scoped 1 topic = 1 case, so thers can faster solutions for their similar case

Just create a new Topic and we will pick you up from there. Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.