简体   繁体   中英

How to retrieve and update JSON value In LogicApp using 'For Each'

Background: I wish to loop through, retrieve and update items within my document

I wish to locate and loop through values within an array, locate nested values and then update them using LogicApps. I know I am on the right path, I just need a little assistance.

1.Here is the document. I wish to loop over A (it has many records in it) and retrieve all of the categories.

         "A": 
               [

                     { 
                       "category": "1"
                     }

               ] 

2.Once I retrieve the categories, I wish to loop over each one of the categories I find and then update their id and status

  "category":"1"
  "box":
      [
         {
            "id": "update"
            "status": "update"

         }
    
      ]

My approach and what I have done:

I believe I have to do a 'For Each' loop over the A's. I am just not sure how to write in the logicApp that I want to retrieve all the "category" fields and then the fields within the box field.

Then once I obtain all the "category" fields, I must update the id and status within this array with a string.

I want to change

   "id":     "update" to    "id" : "number"

and change

    "status": "update"  to   "status": "linked"

I believe I must: initialize, parse, and do a For Loop

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

I believe I must: initialize, parse, and do a 'For Each' Loop, but how do I tell the logicapp I wish to grab the "category" part of the "A" since its nested, and then once I have the "category" part to then update the nested fields of "id" and "status" within that?

Any help is great

You have two options to do the editing.

First is the traditional Logic Apps way:

Use a set of working variables and the setProperty() function to update values as you go along. One variable to hold the portion you are currently editing and an array to hold the work done so far. Since you are working with nested arrays, you'll need two sets of variables- one set for the array inside "A" and one for the array inside "box". In order to update child properties , you just nest setProperty() calls: setProperty(<object>['<parent-property>'], '<parent-property>', setProperty(<object>['parentProperty'], '<child-property>', <value>)) .

If you haven't already, you may want to take a look at this post, as it has a similar premise, though is a bit simpler in structure: Updating Json Array in LogicApp

Second option is to use the inline code action

In your case, I would probably go this route as the number of actions required drops significantly, making the app easier to read and reducing the cost. As long as your data is not too large, you could do all of your edits in a single action and simply output the result. If you want to keep some of the structure or you have a large object, you could keep elements of both- use the loop on your "A" array and then make your changes to that iteration inside Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM