0

I have a csv file and when I convert it using convertto-json i am getting the following error

ConvertTo-Json : The converted JSON string is in bad format. At line:1 char:70 + Get-Content -path E:\test.csv | ConvertFrom-Csv -Delimiter ',' | ConvertTo-J ... + ~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (@{name=WebError.Show; data=1}:PSObject) [ConvertTo-Json], InvalidOperationException + FullyQualifiedErrorId : JsonStringInBadFormat,Microsoft.PowerShell.Commands.ConvertToJsonCommand

when i use -compress with convertto-json i am getting the output, but its a compressed json version which is ugly to see, is there a better way to convert this csv to json or is there a way to decompress the json

CSV:

name,data
Play,http://{gho}.domain.com/
BDomain,domain.com
Charts,2
Compress,0
CompressJ,0
WebError.Show,1

Compressed Json Output:

[{"name":"Play","data":"http://{gho}.domain.com/"},{"name":"BDomain","data":"domain.com"},{"name":"Charts","data":"2"},{"name":"Compress","data":"0"},{"name":"CompressJ"
,"data":"0"}]
3
  • 1
    Please share your JSON with us. We need a reproducable example in order to help you. Commented Apr 21, 2017 at 12:58
  • Why are you piping a Get-Content into a ConvertFrom-Csv? Would it not be easier to use Import-Csv? Commented Apr 21, 2017 at 13:22
  • edited the question with csv and json files Commented Apr 21, 2017 at 14:04

3 Answers 3

0

I don't seem to have any issues using ConvertFrom-Csv and ConvertTo-Json with your example string.

Using:

"name,data
Play,http://{gho}.domain.com/
BDomain,domain.com
Charts,2
Compress,0
CompressJ,0" | ConvertFrom-Csv | ConvertTo-Json

Gives me:

[
    {
        "name":  "Play",
        "data":  "http://{gho}.domain.com/"
    },
    {
        "name":  "BDomain",
        "data":  "domain.com"
    },
    {
        "name":  "Charts",
        "data":  "2"
    },
    {
        "name":  "Compress",
        "data":  "0"
    },
    {
        "name":  "CompressJ",
        "data":  "0"
    }
]

Could you post the PowerShell code which is giving you the error?

Sign up to request clarification or add additional context in comments.

6 Comments

i have this as a file and using import-csv / get-item , both gives the same error
get-content..sorry
When i have a dotted value, it throws this error, WebError.Show, any workaround?
Sorry I don't get the same error when reading from a file, executing Get-Content "test.csv" | ConvertFrom-Csv | ConvertTo-Json works fine. Import-Csv "test.csv" | ConvertTo-Json is also fine for me.
OMG :-o actually this csv was generated from an excel file by using save as, could be some problem there?
|
0
Import-Csv $File | Convertto-Json

This may work for you.

1 Comment

it throws the error when it has to parse WebError.Show
0

Powershell command below creates OUTPUTFILE.json format file from the INPUTFILE.csv file format:

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

Comments

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.