Invalid JSON primitive error converting JSON file

When trying to convert a JSON file through PowerShell:

$json = Get-Content "C:\folder1\test.txt"

$json | ConvertFrom-Json 

write-output $json

I get the following error:

invalid json primitive: [. (System.argunment.exception)

+11
source share
3 answers

I go out on a limb here since you did not provide your input or a complete error message, but I assume that your problem is caused by a format mismatch between the output Get-Contentand the inputs ConvertFrom-Jsonwaiting.

Get-Contentreads the input file into an array of strings, while it ConvertFrom-Jsonexpects JSON data in one line. Furthermore, the conduit $jsonin ConvertFrom-Jsondoes not change the value $json.

, ( ):

$json = Get-Content 'C:\folder1\test.txt' | Out-String | ConvertFrom-Json

Write-Output $json
+15

JSON , "\"

JSON , :

{
    Object1
}
{
    Object2
}

:

[{
     Object1
 },
 { 
     Object2
 }]

, .

+12

You will get this error if your input starts like this:

data: [
  {
    ...
  },
  {
    ...
  }
]

You need to delete data:(and in this example there is only [and ]):

[
  {
    ...
  },
  {
    ...
  }
]
0
source

Source: https://habr.com/ru/post/1546309/


All Articles