In Azure Logic Apps, how can I edit a JSON value within an array of JSON objects?

Polo 0 Reputation points
2023-10-31T13:07:20.3+00:00

I have an array of JSON objects (let's call it array1), which looks like this (notice the repeated lineNum:1) :

[

{

"lineNum":1,

"qty":10

},

{

"lineNum":1,

"qty":20

},

{

"lineNum":2,

"qty": 15

}

]

I want to create a new array of JSON objects that contains the SUM of the "qty" values for each "lineNum". For this example, the resulting array would look like this (let's call it array2):

[

{

"lineNum":1,

"qty":30

},

{

"lineNum":2,

"qty":15

]

I attempted this by reading each of the elements in array1 and either:

  1. appending to array2 when an element with the same lineNum doesn't exist yet (using the "Filter array" data operation and the Append to array action), or
  2. replacing the "qty" value in array2 (by adding to it the new read qty value from array1) when an element with the same lineNum exists (using the "setProperty" expression function).

I can't achieve 2. It looks like I cannot edit the JSON within array2.
How can I achieve this? Inline code maybe? Is there another data structure that I can process by editing its contents?

Thank you so much in advance.

Polo

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,139 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,321 Reputation points
    2023-10-31T15:23:54.5033333+00:00

    Polo Thanks for posting your question in Microsoft Q&A. Yes, you are right. You can simply achieve this with Inline Code action and the following code snippet was generated using AI tool/API service.

    const array1 = [
      {
        "lineNum": 1,
        "qty": 10
      },
      {
        "lineNum": 1,
        "qty": 20
      },
      {
        "lineNum": 2,
        "qty": 15
      }
    ];
    
    const sums = {};
    for (const obj of array1) {
      const lineNum = obj.lineNum;
      const qty = obj.qty;
      if (lineNum in sums) {
        sums[lineNum] += qty;
      } else {
        sums[lineNum] = qty;
      }
    }
    
    const array2 = [];
    for (const [lineNum, qtySum] of Object.entries(sums)) {
      const obj = { lineNum: parseInt(lineNum), qty: qtySum };
      array2.push(obj);
    }
    
    
    return JSON.stringify(array2);
    

    I tested this quickly in my logic app and seems to work fine with the below output.

    Output:

    User's image

    Note: this is just sample code snippet, and you need to modify this for your scenario (like other elements) and validate for all production use cases.

    I hope this helps and let me know if you have any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.