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

Using a function parameter to restore the values of an array

Looking to clear an array using a function parameter. Below is my implementation: <script> fruitArray = ["apple", "banana", "orange", "pineapple"] function clearFruitArray(myArr){ myArr = []; ...

Is it possible to achieve a seamless change using show/hide jQuery functions when hovering over an

How can I achieve smooth transitions when using an image map to show a div on hover, similar to the functionality seen on this website: ? Below is my current JavaScript code: var mapObject = { hover : function(area, event) { var id = area.attr ...

Passing PHP value from a current page to a popup page externally: A guide

I'm currently facing an issue at work where there is a repetitive button and value based on the database. I need to extract the selected value from the current page into a pop-up page whenever the corresponding button is clicked throughout the repetit ...

How to retrieve an element by its class using JavaScript and Jquery while implementing a

I am working on creating a quiz example using Jquery, but I am facing some challenges when trying to manipulate CSS classes with hover in Jquery. .right{ background-color: white; } .wrong { background-color: white; } .right:hover { background-color ...

What is the best approach for addressing null values within my sorting function?

I created a React table with sortable headers for ascending and descending values. It works by converting string values to numbers for sorting. However, my numeric(x) function encounters an issue when it comes across a null value in my dataset. This is th ...

When the state changes, initiate the animation

Currently, I am working with Vue.js and need to animate a navigation menu. My goal is to display two li elements when a user hovers over one of the navigation buttons. At the moment, I have set the data type showActivities to false by default and changed ...

When you scroll through the page, white blocks suddenly appear

After completing a website, I noticed some white blocks appearing when scrolling. You can view the site here. This issue occurs on both mobile and desktop devices during the initial load only. Could this indicate that the page has not fully loaded yet? If ...

When calling UIComponent.getRouterFor, a TypeScript error is displayed indicating the unsafe return of a value typed as 'any'

I have recently integrated TypeScript into my SAPUI5 project and am encountering issues with the ESLint messages related to types. Consider this simple example: In this snippet of code, I am getting an error message saying "Unsafe return of an any typed ...

My AJAX requests do not include any custom headers being sent

I'm facing an issue with making an AJAX request from my client to my NodeJS/ExpressJS backend. After firing the request, my backend successfully receives it but fails to recognize the custom headers provided. For example: $.ajax({ type: " ...

Query an array of objects for partial matches that are not case-sensitive, and then organize the results in alphabetical order

I am seeking assistance or guidance on which topics to explore for answers. As a newcomer to React, I have a code that successfully filters a list within the items object. However, I would like to switch to using a prepared JSON file instead. When I attem ...

Why isn't the background-image displaying with the use of the url() function?

I've been attempting to set an image as the background using background-img:url(imageLing), but it's not working properly. When I inspect the element, it shows me background-image: url(assets/backgrounds/5.jpg);. What could be causing this issue? ...

How to retrieve values from checkboxes generated dynamically in php using jquery

This is a unique question and not related to event binding or any suggested duplicates. Hello, I am facing an issue while trying to fetch the value of a checkbox to send it in an ajax request to a PHP page. The checkboxes are dynamically created using PHP ...

The JSON decoder caught an error due to additional data being present at line 1, column 13 (character 12)

I am currently working on a code snippet that aims to convert a JSON file containing hydrated tweets into CSV format. import pandas as pd import json #Specify the name of the JSON file with open('climate.jsonl') as f: lines = f.read().split ...

Turn off automatic downloading of embedded frame content in Safari for iOS

I am encountering an issue with a modal that displays a PDF and offers two options - one to print and one to download. The download option uses a blob with content-type: application/octet-stream, while the print option utilizes a blob with content-type: a ...

Node.js and Couchbase integration: A closer look at the functionality of the add() method

As I dive into learning about couchbase, I encountered a basic issue with inserting data using the add() method in my couchbase instance. Despite troubleshooting, something seems amiss in my code and I'm unable to pinpoint the exact reason. var http ...

How to set the value of an `<input type="date"` in jQuery

Having trouble figuring out why this code isn't functioning properly. The input field I'm using is a simple 'input type="date"' setup like this.... <input type="date" name="Date"/> I am attempting to have the value automatically ...

Placing jQuery scripts in Blogger platform: A guide

After finding the correct codes to solve my problem in previous questions, such as How do I get an image to fade in and out on a scroll using jQuery?, I came across this helpful code snippet: var divs = $('.banner'); $(window).scroll(function(){ ...

Error animation on client-side validation not resetting correctly

Incorporated a form validation and error display system utilizing TransitionGroup for animations. The flag issueVisible manages the visibility of the error message, while determineField() aids in identifying the field related to the error. The issue arise ...

Substitute "Basic Authentication" with "Form Authentication"

Is there a way in W20 to switch from using "Basic Authentication" to "Form Authentication"? The current documentation mentions only the use of "basicAuth" and does not provide information on implementing form authentication. Our app is built with Angular ...

What is the best way to write an SQL query to safely insert a record into a table with a dynamic name?

I'm working on a function that can insert a record into a table in PostgreSQL. The catch is that the table name needs to be a parameter for the function, and the column names are determined dynamically. To ensure protection against SQL Injection, I am ...