Match and replace the data in Json file array

I want to replace “isProcessed” to ‘true’ after file is processed. How can I search the filename “3 way check INV.pdf” and replace isProcessed to true for that particular array element only?

This is my code in InvokeCode activity:

If (Not String.IsNullOrWhiteSpace(emailDataStr)) Then
emailInvoiceData = JsonConvert.DeserializeObject(Of JObject)(emailDataStr)
End If
’ Check for AttachmentsList JArray exists
If ((emailInvoiceData(“AttachmentsList”) IsNot Nothing) AndAlso (emailInvoiceData(“AttachmentsList”).Value(Of JArray).Count > 0)) Then
emailInvoiceData(“AttachmentsList”).Value(Of JArray).Item(“3 way check INV.pdf”).Replace(TransactionData)
’ Rewrite file
File.WriteAllText(Path.Combine(Path.GetFullPath(Config(“InputInvoiceDir”).ToString), Config(“EmailInvoiceDataFileName”).ToString), emailInvoiceData.ToString)
End If

My Json file has array:
{
“AttachmentsList”: [
{
“fileName”: “test-asia-trade.zip;3 way check INV.pdf”,
“emailDate”: “Tue, 01 Jun”,
“emailFrom”: “noreply@noreply.cloud”,
“emailSubject”: “New Invoice document is received”,
“emailBody”: “test”,
“isProcessed”: false
}
]
}

@ad2000
Variables:


Flow:

Before After
grafik

do not forget to save the changed json e.g. with a write text

@ppr Thanks for your reply.
Am I doing anything wrong? I can’t get value in item(“fileName”).ToString or item(0).ToString

image

@ad2000
the typeArgument of the for each activity has to be configured to JObject (The datatype od the looped item)

we hope that the used json data is the same as mentioned in the post above. So the key fileName is valid.

In case of any issues we suggest to debug and analyzing code statements within the watch panel

@ppr it worked perfectly. Thanks a lot.
Just a question on write text, by using write text activity - it is overwriting whole file rather than just updating one particular array element.
As I have multiple elements in the array, may I know how to modify only value of particular file name?

Serializing elements also won’t work as other array elements are removed from the file.

I resolved the issue using this -

@ppr Thanks a lot for your help.

Exactly, I was about to point that out as well. Since you have a search string value you can update only the required keys. Json is always fun :slight_smile:

write text will dump out the entire data in one rush. There will not be a selective update.
Putting the activity after the for each will avoid to write back part results.

Perfect, just do your final testing and mark the solving post as solution. Others will benefit from it. Thanks

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