0

I need some help with converting a CSV File to Json using PowerShell. I have a script that gets the send message logs from Office365 using PowerShell and export them into a CSV file. I will then use the ConvertTo-Json cmdlet to Convert the CSV file into Json. The output Json file includes so many unnecessary texts that make it hard to read. For example, here is what I want:

{
 "SenderAddress":  "[email protected]",
 "RecipientAddress":  "[email protected]",
 "Subject":  "Microsoft PowerShell",
 "Status":  "Delivered",
 } 

Here is what I get after converting the CSV to Json:

{
        "value":  "\"[email protected]\",\"[email protected]\",\"Microsoft PowerShell\",\"Delivered\"",
        "PSPath":  "C:\\Logs\\Logs-1.csv",
        "PSParentPath":  "C:\\Logs",
        "PSChildName":  "Logs-1.csv",
        "PSDrive":  {
                        "CurrentLocation":  "Windows\\system32",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "Windows",
                        "MaximumSize":  null,
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\test",
                           "Drives":  "C D E"
                       },
        "ReadCount":  3

Here is the PowerShell cmdlet I run to convert the CSV file to Json.

Get-Content "C:\Logs\logs-1.csv" | ConvertTo-Json | Add-Content -Path "C:\Logs\logs-1.json

I also tried the following:

Get-Content "C:\Logs\logs-1.csv" |Select-Object -Property SenderAddress, RecipientAddress, Subject, Status ConvertTo-Json | Add-Content -Path "C:\Logs\logs-1.json

I go this result:

{
    "SenderAddress":  null,
    "RecipientAddress":  null,
    "Subject":  null,
    "Status":  null
}

Any idea what I can do to convert the CSV file to Json correctly without the extra info?

Thanks in advance for any help.

1
  • Can you provide a sample of the CSV log file, including header?
    – Bacon Bits
    Mar 9, 2018 at 16:27

2 Answers 2

3

I'm guessing you need to use Import-Csv not Get-Content or Get-Item.

Try:

Import-Csv "C:\Logs\logs-1.csv" |
    ConvertTo-Json |
    Add-Content -Path "C:\Logs\logs-1.json"

Or:

Import-Csv "C:\Logs\logs-1.csv" |
    Select-Object -Property SenderAddress, RecipientAddress, Subject, Status |
    ConvertTo-Json |
    Add-Content -Path "C:\Logs\logs-1.json"

If you intend to overwrite C:\Logs\logs-1.json, use Set-Content instead of Add-Content.

2
  • Thank you! switching Get-Content to Import-CSV did the trick. I did not know it was that simple.
    – Doug
    Mar 9, 2018 at 16:48
  • As a side note, you can also read CSV from a string using ConvertFrom-CSV. This can be useful, for example, when the CSV is grabbed from remote resource
    – Steve B
    Oct 29, 2021 at 9:17
0

Also, you could define how many levels of contained objects are included in the JSON representation (default depth = 2).

import-csv .\INPUTFILE.csv | ConvertTo-Json -depth 100 | Out-File .\OUTPUTFILE.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.