2

I have no experience with PowerShell and I was asked to create this script as a favor for a friend of mine. The script is supposed to read a csv file (These files have different columns except for time and host, which are common among all files), and output its content into a JSON file of the following format:

CSV file contains columns:

host| message | time | severity | source |

{
"time": 1437522387,
"host": "dataserver992.example.com",
"event": { 
    "message": "Something happened",
    "severity": "INFO",
    "source": "testapp"
    #...All columns except for time and host should be under "event"
    }
}

*The only guaranteed columns are time and host. All other column headers vary from file to file.

This is part of what I have so far:

$csvFile = Import-Csv $filePath


function jsonConverter($file)
{    
    #Currently not in use
    $eventString = $file| select * -ExcludeProperty time, host 


    $file | Foreach-Object {
        Write-Host '{'
        Write-Host '"host":"'$_.host'",'
        Write-Host '"time":"'$_.time'",'

        Write-Host '"event":{'

        #TODO: Put all other columns (key, values) under event - Except for 
        time and host

        Write-Host '}'
    }    
}

jsonConverter($csvFile)

Any ideas of how I could extract only the remaining columns, row by row, outputting its content to a key, value JSON format like the example above? Thank you!

2 Answers 2

4

Provided your csv looks like this:

"host","message","time","severity","source"
"dataserver992.example.com","Something happened","1437522387","INFO","testapp"

this script:

$filepath = '.\input.csv'
$csvData = Import-Csv $filePath

$NewCsvData  = foreach($Row in $csvData){
   [PSCustomObject]@{
       time  =  $Row.time
       host  =  $Row.host
       event = ($Row| Select-Object -Property * -ExcludeProperty time,host)
   }
}

$NewCsvData | ConvertTo-Json

will output this Json:

{
    "time":  "1437522387",
    "host":  "dataserver992.example.com",
    "event":  {
                  "message":  "Something happened",
                  "severity":  "INFO",
                  "source":  "testapp"
              }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I first didn't remember that the parameter -ExcludeProperty requires the parameter -Property to also be present.
Great solution! Thank you so much!
0

If your powershell version is 3.0 or higher (it should):

Import-CSV $filepath | ConvertTo-JSON

Done!

2 Comments

Hey, that doesn't work because "event" is not actually a column in the csv file. "event" is like a key where I have to put all columns except for host and time. This is a customized JSON file. See example above.
Just baked something like @LotPings. But slightly less distinguished. So use his code ;-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.