Avoid writing any duplicate values to a CSV file

I need help with converting a JSON file into a CSV file while avoiding duplicate values in the output. I want to include four columns in my CSV file without repeating any data.

My approach involves storing variables in an array and writing them to a CSV file. However, I'm struggling with preventing duplicates in the 'Name' column. If there is a duplicate entry, I don't want it to be included in the final output.

 ForEach ($fileEnvironment in $jsonVariables.ScopeValues.Environments) { 
        #Create empty array
        $dataArray = @()

      ForEach ($fileVariable in $jsonVariables.Variables) {
         if($fileVariable.Description -eq $null) {
            $fileVariable.Description = 'No Description'
        }
            if (!!$fileVariable.Scope) {
                if (!!$fileVariable.Scope.Environment) {
                    if ($fileEnvironment.Id -eq $fileVariable.Scope.Environment[0]) {                   
                        #Add fileVariable to the array
                        $dataArray += $fileVariable

                    } 
                }
            }
        }

  $filename="$($outputPath)\$($projectName)_$($fileEnvironment.Name).csv"
            if ($dataArray.Count -ne 0) {
                 $dataArray | Select-Object -Property 
  Name,Type,Value,Description | Export-Csv -NoTypeInformation -Path 
  $fileName
        }            

In the resulting CSV file, only unique values should be present in the 'Name' column. For example, we currently have 'apiConfig' repeated, but I want to see just one instance of 'apiConfig' in the CSV:

        Name          Type       Value    Description
        apiConfig     String      ...        No Description
        apiconfig     String      ...        No Description
    

Answer №1

If you're looking to remove duplicates from a list, consider using a HashSet.

Check out this example with console logs:

$originalList = @([PSCustomObject]@{Name="A";Desc="1"},[PSCustomObject]@{Name="A";Desc="2"},[PSCustomObject]@{Name="B";Desc="3"})

▷ $originalList

Name Desc
---- ----
A    1
A    2
B    3

▷ $set = New-Object "System.Collections.Generic.HashSet[string]"
▷ $uniqueList = $originalList |? {$set.Add($_.Name)}
▷ $uniqueList

Name Desc
---- ----
A    1
B    3

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

What is the standard way to separate multiple objects within a single JSON file?

Currently, I am dealing with a RESTful API that delivers JSON messages; however, there is a level of uncertainty involved due to the unavailability of the API. Although I have a rough specification, some details are missing which leads me to assume the API ...

Two-step REST service: Achieving a 200 OK or 201 Created status

As part of my work on an e-commerce application, I am developing a service that will allow customers to change the address for an existing order. This process involves two steps: Step 1: The GUI will send a POST request with the orderNumber as a path vari ...

Fusion Tables API: JavaScript Integration - resolvable typeError encountered when accessing API in browser

I've been working on extracting data from a Google Fusion Table. Following the Google documentation, I created a JSON file by using the Fusion Tables API with this call: *%20FROM%yyyyyy&key=xxxxxxxxx" Now, I want to integrate this call into a w ...

Using Powershell to open a new terminal and execute a command in a bash session within WSL

Currently, I am in the process of creating a Powershell script to expedite setting up my development environment. My goal is to have multiple instances of WSL programs running on bash terminals. My objectives from within Powershell are as follows: Open a ...

Back up the essential data of the core system, focusing on just one

One of my main goals for my application is to enable data backup and exchange between users. Specifically, I am looking to export a single entity without having to export the entire database. While I have come across some resources on backing up the entir ...

Differentiating Between Arrays and Objects in AngularJS ng-repeat

When using ng-repeat to display data in a View from an endpoint in the form of an atom feed, I encountered an issue. The endpoint returns JSON if the Accept header is 'application/json', but when there is only one entry in the atom response, the ...

The JSON data is not showing the ID information

I am currently working on a project where I need to pull data from a JSON file and display it on a website. However, I am facing an issue where the ID is not being displayed for some reason. Can someone provide guidance on how to fix this? function getJ ...

The contents of a Javascript array are not appearing within a div element

I have developed a program that reads JSON data related to a concert event. The JSON file consists of an object named global, which includes details about the band name and venue. Additionally, there is a tickets object that contains information on all ava ...

URL for requesting JSON data using Zend Framework

Just getting started with ZF1 and I'm tackling the task of obtaining a JSON response for a specific id. I have successfully retrieved all values from a table using the code below. The question now is, how can I pass a parameter from the URL? public ...

What factors should be considered when deciding whether to create an entity in Django using Tastypie, based on data from another entity?

In my Event class, I have defined the number of participants and the participant limit. Additionally, there is a Ticket class which represents registration for the Event. I need to ensure that tickets for an event are not created if the event has reached ...

What is the best way to transform a JsonWriter into a JsonObject using GSON?

Looking for assistance on converting JsonWriter to JsonObject in GSON without using any predefined object. JsonWriter writer = new JsonWriter(new FileWriter("C:\\Users\\ravr\\Desktop\\outputJSONSChema.json")); ...

A guide on showcasing JSON format in a table view interface

Recently, I have ventured into the world of IOS development and I am faced with a task to showcase JSON fetched data in a UITableView. The JSON structure that I am working with looks something like this: {"output":[{"id":{"php":"1"},"name":{"php":"ramkuma ...

Using AngularJS for AJAX operations with dynamic PHP variables

I have an AngularJS code that is looping through an AJAX JSON response like this: <div ng-repeat="post in posts"> {{post.title}} {{post.url}} </div> It's working fine. How can I pass a PHP variable based on the JSON? Let's assume t ...

Transform the Data into JSON format

I need assistance converting my data into the correct JSON format. The current structure of my data is as follows: [ "{ id:001, name:akhilesh, }", "{ id:002, name:Ram, }" ] My goal is to transform the above data into valid J ...

Transforming a nested array of objects in JSON into a flattened Excel format using Python

Converting Nested JSON Array to Flattened Excel using Python Code Snippet: def read_json(filename): jsonData = {} try: with open(filename, "r", encoding="utf-8") as f: jsonData = json.loads(f.read()) except: raise Exception(f"Reading {filename} file enco ...

How can I understand the result if JSON data is getting returned as undefined? What is the solution to

I am currently dealing with a dataset that contains location data. I have successfully transformed this data into latitude and longitude values through the GetLocation method. However, upon returning to the html page, the data appears to be undefined. Can ...

Tips for Retrieving the Key Names of JSON Objects within a JSON Array

I'm trying to retrieve the object names "channelA" and "channelB". Here is the JSON data: [ { "channelA": { "programmes": [ { "start_utc": 1522208700, "stop_utc": 152220 ...

Leveraging PapaParse for CSV file parsing in a React application with JavaScript

I am encountering an issue with reading a CSV file in the same directory as my React app using Javascript and Papaparse for parsing. Below is the code snippet: Papa.parse("./headlines.csv", { download: true, complete: function(results, f ...

The WCF response has been deemed invalid because of the presence of an unexpected string combination involving WCF and JSON

I recently set up a WCF service to interact with JSON data, but I'm encountering an issue where all entries are being escaped. [ { "rel":"http:\/\/localhost:3354\/customer\/1\/order", &qu ...

Transforming an Excel document into JSON format: inspection of JSON code structure

Looking to transform Excel data into JSON format, but unsure if the current structure will be ideal for processing with D3.js. Planning to incorporate this JSON file within D3.js for visualization purposes. Here's a snippet of the Excel file: In the ...