简体   繁体   English

如何使用“For Each”在 LogicApp 中检索和更新 JSON 值

[英]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.我希望找到并循环遍历数组中的值,找到嵌套值,然后使用 LogicApps 更新它们。 I know I am on the right path, I just need a little assistance.我知道我走在正确的道路上,我只需要一点帮助。

1.Here is the document. 1.这是文件。 I wish to loop over A (it has many records in it) and retrieve all of the categories.我希望遍历 A(其中有很多记录)并检索所有类别。

         "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 2.一旦我检索到类别,我希望循环遍历我找到的每个类别,然后更新它们的 id 和状态

  "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.我相信我必须在 A 上做一个“For Each”循环。 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.我只是不确定如何在我想要检索所有“类别”字段的 logicApp 中编写,然后是框字段中的字段。

Then once I obtain all the "category" fields, I must update the id and status within this array with a string.然后,一旦我获得所有“类别”字段,我必须使用字符串更新此数组中的 id 和状态。

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我相信我必须:初始化、解析并执行 For 循环

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

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?我相信我必须:初始化、解析并执行一个“For Each”循环,但是我如何告诉逻辑应用我希望从嵌套的“A”中获取“类别”部分,然后一旦我有了“类别”部分然后更新其中的“id”和“status”的嵌套字段?

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.使用一组工作变量和setProperty() function 在 go 中更新值。 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".由于您正在使用嵌套的 arrays,因此您需要两组变量——一组用于“A”内的数组,一组用于“box”内的数组。 In order to update child properties , you just nest setProperty() calls: setProperty(<object>['<parent-property>'], '<parent-property>', setProperty(<object>['parentProperty'], '<child-property>', <value>)) .为了更新子属性,您只需嵌套setProperty()调用: 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如果你还没有,你可能想看看这篇文章,因为它有类似的前提,但结构更简单一些: 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.在您的情况下,我可能会 go 这条路线,因为所需的操作数量显着下降,使应用程序更易于阅读并降低成本。 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.只要您的数据不太大,您就可以在一个操作中完成所有编辑,只需 output 结果。 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.如果你想保留一些结构或者你有一个大的 object,你可以保留两者的元素——在你的“A”数组上使用循环,然后在 Javascript 中对迭代进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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