FIXED – Cannot convert the JSON string … contains the duplicated keys ‘Id’ and ‘ID’

Recently I came across the error message below when download a SharePoint 2013 List item REST api call to PowerShell.  The cmdlet ConvertFrom-JSON does an excellent job.  However, it is case sensitive with property names.

The resolution I found was to first string replace (case sensitive) to rename one of the source property names.  Cheers!  shades_smile

Screenshot – Before

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys ‘Id’ and ‘ID’.

image

Screenshot – After

image

Code

cls
# Config
$web = "http://portal/sites/team"
$list = "Test"
# Digest
$urlView = "$web/_api/contextinfo"
$urlView
$req = Invoke-WebRequest $urlView -UseDefaultCredentials -UseBasicParsing -Method Post
$ctx = $req.Content
$digest = $ctx.GetContextWebInformation.FormDigestValue
$digest
# List Items
$urlItems = "$web/_api/web/lists/GetByTitle('$list')/getitems"
$json = "application/json; odata=verbose"
$headers = @{"Accept" = $json; "Content-Type" = $json; "X-RequestDigest" = $digest}
$body = '{"query":{}}'
$req = Invoke-WebRequest $urlItems -UseDefaultCredentials -UseBasicParsing -Method Post -Body $body -Headers $headers
$resp = $req.Content
### FIX - Case Sensitive Property Name Replacement ###
$resp = $resp.Replace("Id","Idd")
$json = $resp  | ConvertFrom-Json
$json

References

Return to Top ▲Return to Top ▲