How to convert json to csv inside logic app inline javascript

Shama 0 Reputation points
2024-02-19T18:57:54.55+00:00

Hi I am new to azure logic app and recently there is a requirement to convert incoming messages from service bus queue to csv. There are going to be 500 records. We have already js code to convert to csv and mapped to fields as per our mapping file. But in my case the json records coming are multiple record and the existing code expects only one object. I want a javascript code to handle multiple records. Uploading my input json as well as the existing js code. Can anybody help me with js code pleaseIMG-20240220-WA0002

Below is the existing js code we useIMG-20240220-WA0003

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

2 answers

Sort by: Most helpful
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2024-02-19T20:04:04.2566667+00:00

    Thanks for posting your question in the Microsoft Q&A forum. May this model can help you to do dynamic assignment

    function jsonToCsv(records) {
        // CSV header (extract field names from the first record)
        var header = Object.keys(records[0]).join(",");
        var csv = header + "\n";
    
        // Iterate over each record and add to CSV string
        records.forEach(function(record) {
            var values = Object.values(record).join(",");
            csv += values + "\n";
        });
    
        return csv;
    }
    
    
    

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **

    0 comments No comments

  2. MikeUrnun 9,777 Reputation points Moderator
    2024-02-21T21:27:27.9066667+00:00

    Hello @Shama - Thanks for reaching out, and engaging us on the MS Q&A community.

    Logic Apps provides an easier way to handle the JSON to CSV conversion, please review the following doc: Create CSV table action

    We recommend using the solution described in that doc, and moving away from the Javascript-based approach which may be error-prone and slow for large data sets. Inline Code is great for quick, stateless code/logic.

    Hope this helps. Feel free to tag me in the comments below if you have any follow-up questions


    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.

    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.