2

I have a CSV file that I am trying to convert to JSON using PowerShell.

The CSV file contains the following data.

web_url.csv

wikipedia,https://en.wikipedia.org/wiki/%s
wolframalpha,http://www.wolframalpha.com/input/?i=%s
drive,http://www.drive.google.com/

I would like to convert to json in the following format. Similarly how do you convert this json back to original csv in the format shown above?

web_url.json

{
    "wikipedia": "https://en.wikipedia.org/wiki/%s",
    "wolframalpha": "http://www.wolframalpha.com/input/?i=%s",
    "drive": "http://www.drive.google.com/"
}

When I run the command,

Get-Content -path web_url.csv | ConvertFrom-Csv -Delimiter ',' | ConvertTo-Json

it returns the following output which is not what I want.

[
    {
        "wikipedia":  "wolframalpha",
        "https://en.wikipedia.org/wiki/%s":  "http://www.wolframalpha.com/input/?i=%s"
    },
    {
        "wikipedia":  "drive",
        "https://en.wikipedia.org/wiki/%s":  "http://www.drive.google.com/"
    }
]

3 Answers 3

12
# PowerShell script

import-csv "SampleInput.csv" | ConvertTo-Json | Add-Content -Path "output.json"
Sign up to request clarification or add additional context in comments.

Comments

3

Your csv doen't look like a "proper" csv to me: columns are swapped with rows. If you have control over input file, you may fix it there already:

@'
wikipedia,wolframalpha,drive
https://en.wikipedia.org/wiki/%s,http://www.wolframalpha.com/input/?i=%s,http://www.drive.google.com/
'@ | ConvertFrom-Csv | ConvertTo-Json

If that is not possible, you just have to perform some extra steps to get what you need:

$propertyList = @'
wikipedia,https://en.wikipedia.org/wiki/%s
wolframalpha,http://www.wolframalpha.com/input/?i=%s
drive,http://www.drive.google.com/
'@ | ConvertFrom-Csv -Header Name, Value

$properties = [ordered]@{}

foreach ($property in $propertyList) {
    $properties.Add($property.Name, $property.Value)
}

New-Object PSObject -Property $properties | ConvertTo-Json

And back, again - some extra work is required:

(@'
{
    "wikipedia":  "https://en.wikipedia.org/wiki/%s",
    "wolframalpha":  "http://www.wolframalpha.com/input/?i=%s",
    "drive":  "http://www.drive.google.com/"
}
'@ | ConvertFrom-Json).PSObject.Properties |
    Select-Object Name, Value |
    ConvertTo-Csv -NoTypeInformation |
    Select-Object -Skip 1

Comments

0

what about this ?

$csv=@"
wikipedia,https://en.wikipedia.org/wiki/%s
wolframalpha,http://www.wolframalpha.com/input/?i=%s
drive,http://www.drive.google.com/
"@

$obj= $csv |convertfrom-csv -delim ',' -Header "name","url"
#$obj

$json=$obj  |convertto-json 
#$json

$csv2 =$json |convertfrom-json |select -expand syncroot  |convertto-csv -NoTypeInformation -Delimiter ','
#$csv2

1 Comment

The JSON you get is [ { "name": "wikipedia", "url": "en.wikipedia.org/wiki/%s" }, { "name": "wolframalpha", "url": "wolframalpha.com/input/?i=%s" }, { "name": "drive", "url": "drive.google.com" } ] which is not what I want. See the JSON example I have in my question.

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.