Transform an object into an array using JavaScript with the help of Lodash, Azure Functions, and Azure Logic Apps

To achieve the desired result of extracting JSON from a proprietary content management system, transforming it into a CSV, and depositing that CSV in an Office 365 shared drive, a combination of Azure Function and Azure Logic App is utilized. The Node/JavaScript Azure Function successfully retrieves the JSON object and sends it to the Azure Logic App for further processing.

The Logic App includes a built-in JSON-to-CSV "action" that requires the input to be in an array format.

Despite various attempts, converting the JSON object into an array has been unsuccessful. Experimentation with tools like node-jq and Lodash have not yielded the desired outcome.

Original JSON:

[
    {
        "Challenge": {
            "Group": {
                "Name": "Challenge group name"
            }
        },
        "Name": "Name",
        ...
<99 more>
]

Desired result:

Modifications required on specific keys within the array for better readability.

[
    {
        "Group": "Challenge group name" (equivalent to Challenge.Group.Name),
        "Title": "Name" (equivalent to Name),
        ...
<99 more>
]

Azure Function code

A sample implementation using Azure Function and related dependencies for data extraction and transmission operations.

(Detailed code example omitted)

Failed attempts (a small number)

Attempt

var flat = _.flatMap(result, 'Name');

Result

["123","456","789","012","345",<etc>]

Attempt

const arr = (result, keyAs) => _.values(_.mapValues(result, (value, key) => { value[keyAs] = key; return value; }));

Result

undefined

Attempt

result.blocks = _(result.blocks)
.map('Name')
.value();

Result

result is not defined

Attempt

_.map(info, (obj, key) => {
obj.symbol = key
return obj
 })

Result

No discernible effect observed.

Note: As a non-professional coder seeking assistance, any guidance or support is greatly appreciated.

Answer №1

It seems like aside from Azure context, you are looking to transform your original JSON into a specific (flat) format.

Here's a simple solution that doesn't require any external libraries:

converter-flat-json.js

const sourceData = [
    {
        "Challenge": {
            "Group": {
                "Name": "Challenge group name"
            }
        },
        "Name": "Name",
        "Description": "Description",
        "CreatedDate": "2020-10-01",
        "Url": "https://url",
        "Category": {
            "Name": "Category name"
        },
        "TotalVotes": 1,
        "YesVotes": 2,
        "NoVotes": 3,
        "CurrentStatus": {
            "Status": {
                "Name": "Current status status name"
            },
            "Author": {
                "DisplayName": "Current status author display name"
            },
            "CreatedDate": "2020-10-01"
        }
    },
]

const flatData = sourceData.map((item) => ({
    Group: item.Challenge.Group.Name,
    Title: item.Name,
    User: item.CurrentStatus.Author.DisplayName,
    Category: item.Category.Name,
    Status: item.CurrentStatus.Status.Name,
    CreatedDate: item.CreatedDate,
    TotalVotes: item.TotalVotes,
    YesVotes: item.YesVotes,
    NoVotes: item.NoVotes,
    URL: item.Url,
}))

console.log(flatData)

outputting:

 ~ $ node convert-to-flat-array.js 
[
  {
    Group: 'Challenge group name',
    Title: 'Name',
    User: 'Current status author display name',
    Category: 'Category name',
    Status: 'Current status status name',
    CreatedDate: '2020-10-01',
    TotalVotes: 1,
    YesVotes: 2,
    NoVotes: 3,
    URL: 'https://url'
  }
]

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

Use the Fetch() function in Google Sheets to parse JSON data and organize it into

I have been attempting to utilize the Fetch() function in order to import a json file and populate multiple Google sheets. However, I have encountered some difficulties as my current knowledge is insufficient. The json file I am working with is constantly ...

Encountered an Xpath error while attempting to create a random email generator using Selenium IDE

While attempting to execute a script, I encountered an "element not found" error with Selenium failing to detect the xpath. My goal is to randomly generate email addresses. Every time there is an error message stating: [error] Element .//[@id='GmailA ...

Tips for seamlessly integrating XHP and ReactJS for a component's implementation

Imagine this scenario: a blog with a posts feed. Upon loading the page, three <PostCard>s are already loaded from the server-side. As the user scrolls down or clicks a Load more button, new post cards should be dynamically added to the page. We have ...

The search box output will be the same as the JSON result

I have a server in Node.js that is able to read and process a JSON file containing various data, including unique user IDs. I have incorporated a search box into my HTML page, and I am seeking assistance with creating a jQuery method (which will require AJ ...

Tips on how to customize/ng-class within a directive containing a template using replace: true functionality

To keep replace: true, how can ng-class be implemented on the directive below without causing conflicts with the template's ng-class? This currently results in an Angular error: Error: Syntax Error: Token '{' is an unexpected token at co ...

What is the process for executing PhantomJS commands through a NodeJs server?

My current challenge involves setting up a Node Server and incorporating PhantomJS commands within the NodeJS server. An example command includes: phantomjs phantom-server.js http://example.com Although I found some information related to this issue on ...

Is there a way to invoke a function once grecaptcha.execute() has completed running, but in response to a particular event?

Presently, the JavaScript function grecaptcha.execute is triggered on page load, as shown in the first example below. This means that the reCAPTCHA challenge occurs as soon as the page loads. A more ideal scenario would be to trigger it when the form submi ...

How can I use AngularJS to show a JSON value in an HTML input without any modifications?

$scope.categories = [ { "advertiser_id": "2", "tier_id": 1, "tier_name": "1", "base_cpm_price": "", "retarget_cpm": "", "gender": "", "location": "", "ageblock1": "", "ageblock2": "", "ageblock3": ...

In PHP, what is the best way to close the HTML tags found in this array?

I am struggling with an array that contains HTML tags and I need to create a formatted output using a PHP function. However, I am having trouble closing the tags correctly and would appreciate some assistance. The existing array is structured line by line ...

Parsing JSON data with dynamic nested property names

Looking for advice on deserializing a complex JSON response from an external REST API using System.Text.Json in .NET 6. Here's the situation: I have a model defined for the data: class DeviceData{ //lots of properties } When querying for a singl ...

Ajax - Retrieving data from a different webpage

My index.php contains the following HTML: <div id="showDetails"> </div> <div id="showList"> </div> And this Ajax function is also in index.php: function ...

"Step-by-step guide on uploading multiple images to a Node server and storing them in

Hey everyone! I'm currently working on a project using React and MongoDB. Users are required to register and login before accessing the app. Once logged in, they can input their name, number, and images through a form. However, I've encountered a ...

Select the five previous siblings in reverse order using jQuery's slice method

I have a unique approach to displaying a series of divs where only 5 are shown at a time using the slice() method. To navigate through the items, I include an ellipsis (...) link after every 4th item, which allows users to easily move on to the next set of ...

Chrome and Firefox provide excellent compatibility for running JavaScript, whereas Safari may encounter some issues. Opera's performance with JavaScript can be quirky

Disclaimer: I'm new to web design and development. I have encountered an issue with posting information from a form built on CodeIgniter using jQuery. The form posts successfully in Chrome and Firefox, with the current page automatically reloading. H ...

How can you start a jQuery script following a triumphant Ajax callback?

Having some trouble getting jQuery to execute after a successful Ajax response. Even though I have it in the success callback, it doesn't seem to be working as expected. I came across a potential solution here, but I'm having difficulty understan ...

Attempting to convert JSON date format in order to send it to a different system via a POST request in

My goal is to create a script that fetches project data from Insightly and posts it on 10000ft. The idea is to transfer any new projects created in one system to another system, both of which have the 'Project' concept. I am a beginner at this a ...

FitText.js malfunctioning

I'm currently experimenting with using FitText.js to dynamically adjust the size of headlines to fit within the limits of the browser width. Interestingly, while this script successfully resizes the text in multiple sections of my website, it seems t ...

Troubleshooting: The Google Analytics Universal Event Tracking Code is not functioning as

I am having trouble tracking clicks on an image that links to another site in a HTML widget on my website’s sidebar. I have implemented Google Analytics code, but for some reason, the clicks are not showing up in the "Events" tab of my analytics dashboar ...

Tips for presenting random images from an assortment of pictures on a webpage

I'm looking to enhance my website by adding a unique feature - a dynamic banner that showcases various images from a specific picture pool. However, I'm unsure of how to find the right resources or documentation for this. Can you provide any guid ...

Utilizing jQuery AJAX to Send an HTML Array to PHP

In my current HTML forms and jQuery AJAX workflow within the Codeigniter Framework, I've encountered a common issue that has yet to be resolved to suit my specific requirements. Here's the situation: HTML - The form includes an array named addre ...