Creating separate objects and arrays by splitting JSON data in LogicApp through JavaScript

I am receiving JSON data from a source system into my Logic App. Here is an example of the data:

[
   {
      "project":"abc",
      "assignees":"123,456"
   },
   {
      "project":"xyz",
      "assignees":"123,468"
   }
]

My goal is to split the "assignees" and create arrays within objects to generate the following final output:

[
   {
      "metadata":{
         "type":"project"
      },
      "name":"Project ABC",
      "assignee":[
         {
            "metadata":{
               "type":"assignment"
            },
            "employeeId":"123"
         },
         {
            "metadata":{
               "type":"assignment"
            },
            "employeeId":"123"
         }
      ]
   },
   {
      "metadata":{
         "type":"project"
      },
      "name":"Project ABC",
      "assignee":[
         {
            "metadata":{
               "type":"assignment"
            },
            "employeeId":"123"
         },
         {
            "metadata":{
               "type":"assignment"
            },
            "employeeId":"468"
         }
      ]
   }
]

Is it possible to achieve this transformation using only Logic Apps? If not, how can I accomplish this with inline JavaScript code?

Answer №1

I have no prior knowledge of LogicApp or its functionality, but if you just need JavaScript code for this specific transformation, here is an example:

const data=[
   {
      "project":"abc",
      "assignees":"123,456"
   },
   {
      "project":"xyz",
      "assignees":"123,468"
   }
]
const transformedData=data.map(item=>({
                        "metadata":{type:"project"},
                        name:"Project "+item.project.toUpperCase(),
                        assignee:item.assignees.split(",").map(assignee=>({
                                            metadata:{type:"assignment"},
                                            emplyeeId:assignee
                                            }))
                        }))

Answer №2

I set up a variable called source to store the same data source as yours.

https://i.sstatic.net/i0loX.png

Here is a sample of inline javascript code for your reference:

var source = workflowContext.actions.Initialize_variable.inputs.variables[0].value;

var result = [];

source.forEach(sItem=>{
    var resultItem = {
        "metadata":{
         "type":"project"
        },
        "name":"Project " + sItem.project.toUpperCase()
    }

    var assignee = [];
    var assigneesSplit = sItem.assignees.split(",");
    
    assigneesSplit.forEach(item=>{
        var assigneItem = {
            "metadata":{
               "type":"assignment"
            },
            "employeeId":item
        }
        assignee.push(assigneItem);
    });
    resultItem.assignee = assignee;
    
    result.push(resultItem);
});

return result;

Upon completion of running the logic app, the resulting data will look like this:

[
  {
    "metadata": {
      "type": "project"
    },
    "name":: "Project ABC",
    "assignee"[:
      {
        "metadata":{
          "type""}: "assignment"
        },
        "employeeId"":: "123"
      },
      {
        "metadata":{
          "type"}": "assignment"
        },
        "employeeId";: "456"
      }
    ]
  },
  {
    "metadata"":::{
      "type"";: "projec""
    },
    "name""::"": "Proje XYZ"",
    "assignee"w:[
      {
        "metadata":{
          "type"}:: "assignment "}
        },
        "employeeId"":: ": "123"
      },
      {
        "metadata"":":{
          "type"`": "assignmen"
        },
        "employeeId";: "468"
      }
    ]
  }
]

If there are any mistakes in your expected data samples (such as the second project name and the second employeeId in the first assignee field), please inform me so I can adjust my js inline code accordingly to meet your desired json data.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting connection timeouts between node.js and amqp

In my project, I am utilizing RabbitMQ through the AMQP module in Node.js with 2 producers and 2 consumers. Here is the code snippet for establishing connections for the consumers: function init_consumers( ) { console.log( 'mq: consumers connect ...

Issue with collapsed side-menu in Vue using Element-UI

Attempting to create a basic admin panel using the Element UI library has presented me with a challenge. The issue arises when the sidemenu is hidden, resulting in the content not occupying 100% of the space. To resolve this, I found that disabling menu a ...

embedding fashion within offspring component in vue

Looking to apply inline styles to a child component? I am trying to pass properties like height: 200px and overflow-y: scroll. I attempted passing it this way: <Childcomponent style="height: 200px; overflow-y: scroll;" /> but had no luck. ...

Is it possible to utilize the onError attribute for activating an HTML data-remodal-target?

I've been working on a new website and recently incorporated Remodal into it. When using data-remodal-target to trigger modals, I noticed that if there is an error like a missing image or stylesheet, the site may not function properly. I am looking fo ...

Amazon Lex: Transforming Speech to Text with Audio Technology

Is there a JavaScript SDK provided by Amazon for converting audio files to text using Amazon Lex? I am currently working on a Node.js application and would like to achieve this functionality. ...

Adding a Javascript value to a WebSQL database

Trying to store the total values of various select lists and radio buttons in a websql database. The initial form inserts correctly, but everything breaks when attempting to add the total variable. Using getElementByID("total") to add it to the table does ...

Create a custom sorting option for an Angular select dropdown menu that is tailored to a specific

<select name="region" id="region" ng-options="id as name for (id, name) in regions[entry.country]" ng-model="entry.region" class="form-control select-styled" required></select> I am looking to implement a filter or custom sort to rearrange th ...

Troubleshooting: PHP AJAX Image Upload Issue - Images Not Being Uploaded

I am having an issue with my code. Although it doesn't show any errors in the console, clicking the upload button does not trigger any action. Following a tutorial, I tried to send a post request to itself, but the image is neither uploaded to the des ...

Tips for making an input form that triggers an alert popup

After creating an HTML form with text input, utilizing Javascript for validation as shown below: I am trying to trigger an alert box thanking the user for entering data when the submit button is clicked. I have faced challenges in implementing this witho ...

Google AdSense is unable to detect code fragments within the header of a Next.js website

Hello, I've been trying to set up Google AdSense on my Next.js site, but AdSense doesn't seem to recognize it. I keep receiving an error saying that I need to place the code inside my headers. How can I properly implement this? Below is my _docum ...

What is causing my cookie script to malfunction?

I'm experiencing an issue with my cookie script on the following website: Upon visiting my site, you'll come across a hidden <div id="popupDiv"> Due to the display: none property in the CSS, it's not visible as intended. However, it ...

When working on a Vue project, the Navbar @click functionality seems to

Having trouble with my navbar search form <form action="" method="post" class="search"> <input type="text" name="" placeholder="поиск" class="input" v-model="alls ...

Do AngularJS applications function similarly to windows?

Do AngularJS apps behave like windows? And is it possible to add an event listener to them? I am currently working on a proof of concept to see if an Angular app can communicate with a server without impacting the host. One potential solution I have thou ...

Updating the Progress Bar in Shopify when the "Add to Cart" button is clicked: A step-by

I'm currently working on implementing a shipping progress bar on my Shopify Theme. However, I've encountered an issue where the progress bar does not update correctly unless I refresh the page along with the content text. Can anyone provide guid ...

What is the reason for JSON.parse throwing errors on the same strings that eval does not?

Imagine having something like this: var a = '["\t"]' When you use: eval('var result = ' + a) Everything runs smoothly. However, if you try: var result = JSON.parse(a) You'll encounter an error: Unexpected token. The s ...

Discovering documents using the outcome of a function in mongoose

Hey there, I have a scenario with two schemas known as user and driver, both containing latitude and longitude attributes. My goal is to search the database for nearby drivers based on the user's current location (latitude and longitude) using a custo ...

How can union types be used correctly in a generic functional component when type 'U' is not assignable to type 'T'?

I've been researching this issue online and have found a few similar cases, but the concept of Generic convolution is causing confusion in each example. I have tried various solutions, with the most promising one being using Omit which I thought would ...

Authentication through Google and Twitter

I'm looking to incorporate Google and Twitter login buttons on my website without the need for server-side code. Unfortunately, I haven't been able to find any APIs that allow for this without a secret key. Do you have any suggestions or ideas on ...

Check and confirm that the request object is being sent to PostAsJsonAsync

Is there a way to verify the request object passed to HttpClient.PostAsJsonAsync? The request object is created within the method being unit tested, so I need to ensure that it has the correct values. I have attempted the following code and I am trying to ...

The order of event capturing and bubbling phases is FLIPPED for the node triggering the event

Note: Using Chrome, works as expected in Safari. In summary, I thought I had a good knowledge of JavaScript. To test my skills, I decided to take a challenge and guess what happened: We have an <input type="button" id="btn"> with two event handlers ...