24

When trying to convert a JSON file via PowerShell:

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

$json | ConvertFrom-Json 

write-output $json

I'm getting the following error:

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

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user3770612
  • 1,649
  • 7
  • 20
  • 30
  • 1
    Sorry for asking about this after a long time, but did you find an appropriate solution? I am facing a similar issue, with the period(.) being the invalid json primitive. I tried all the formats of json in the answers below. None worked. TIA – Sanjiv Pradhanang Feb 23 '21 at 17:41

4 Answers4

21

I'm going out on a limb here, since you didn't provide your input data or the complete error message, but I guess that your problem is caused by a format mismatch between the output Get-Content provides and the input ConvertFrom-Json expects.

Get-Content reads the input file into an array of strings, whereas ConvertFrom-Json expects the JSON data in a single string. Also, piping $json into ConvertFrom-Json does not change the value of $json.

Change your code to the following and the error should disapear (provided there is no syntactical error in your input data):

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

Write-Output $json
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 2
    Get-Content will include newline characters, so you'll want to either pass a -join like this: $json = (Get-Content 'C:\folder1\test.txt') -join "`n" | ConvertFrom-Json or simply pass the -Raw switch: $json = (Get-Content 'C:\folder1\test.txt') -Raw | ConvertFrom-Json Reference: https://technet.microsoft.com/en-us/library/Hh849898.aspx#code-snippet-4 – Derek Nutile Aug 04 '15 at 23:19
  • 2
    @DerekNutile `Get-Content` produces an array of strings (one for each line from the file), which must be transformed into a single string before feeding the data into `ConvertFrom-Json` (otherwise the cmdlet would try to convert each line separately). The transformation can be achieved in several ways, for instance by joining the array with newline characters, by reading the file using the `-Raw` parameter (PowerShell v3 and newer only), or by piping the output through `Out-String` first, as in my answer. – Ansgar Wiechers Aug 05 '15 at 06:57
14

You should check your JSON input file for characters that are not properly escaped with a "\"

I have also seen this issue with an input JSON file that was incorrectly formatted as follows:

{
    Object1
}
{
    Object2
}

Corrected format:

[{
     Object1
 },
 { 
     Object2
 }]

Once the format was corrected, I had no more issues.

itrjll
  • 186
  • 1
  • 5
  • 3
    It seems the Powershell cmdlet ConvertFrom-JSON does not like JSON that does not contain opening and closing square brackets [ ] around arrays. You receive a Invalid Json primitive error. As per this answer I added square brackets and all was well. Thanks. – CarlR Dec 08 '15 at 18:08
4

I was also receiving this error, and upon investigating my json file noticed that some of the JSON was invalid. I was ending the last object in an array with a comma like so:

[{ ..},]

Removing the comma fixed the issue for myself.

So in short, invalid JSON caused this issue for me.

bpilling
  • 183
  • 9
  • Excellent answer. I found this to be the case when I copied a coded object in a script, and replaced the semicolons (;) with commas (,) for use in a file instead of in the script definition. The last one should have been removed since there was nothing else following. – Donald.M Jul 13 '20 at 19:23
1

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

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

You need to remove data: (and only have [and ] in this example):

[
  {
    ...
  },
  {
    ...
  }
]
uranibaba
  • 712
  • 1
  • 13
  • 30
  • 2
    How do you remove that with a cmdlet? – Prayank Aug 02 '21 at 19:34
  • 1
    @Prayank I made a new question with an answer for your question instead of answering in the comments. https://stackoverflow.com/questions/73464292/how-to-remove-data-from-the-beginning-of-a-json-file-with-powershell/ – uranibaba Aug 23 '22 at 19:39