loremIpsum1771 loremIpsum1771 - 4 months ago 35
JSON Question

Parsing error when trying to validate JSON file in JSLint

I've created a powershell script that creates a JSON file which lists all of the files in a directory along with their size and hash codes (I know there are more efficient ways to do this but its required). For some reason, in the object that holds the file information, the first line is returning the error:
Error: Parse error on line 8:

... { "fileName": ".\app.config", "
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'


It seems clear that the attribute is of type string, but for some reason, the parser can't resolve this. What could be causing the error with the type not being determined?
The JSON file is below for reference:

Update:



The JSON file seems to be generating correctly for the most part but for some reason, the
name
and
version
attributes are being added to the end of the file instead of the beginning. Here is the powershell function for reference:

function GenerateManifest([string] $manifestPath, [string] $packageName, [string] $version, [string] $hashCode, [string] $fwVersion, [string] $outputPath)
{
"{" > $manifestPath

$obj = @{
name = $packageName
version = $version
frameworkVersion = $fwVersion
files = @()
}

if($outputPath)
{
cd $outputPath
$files = Get-ChildItem -recurse | Where {($_.FullName -notlike $manifestPath) -And (-not ($_ -is [System.IO.DirectoryInfo]))}

if($files)
{
"""files"": [ " >> $manifestPath
foreach($file in $files)
{
if($file.name -eq $manifestPath)
{
continue;
}
$fileHash = GetFileHash($file.FullName)
$filePath = $file | resolve-path -relative

$fileObj = @{
fileName = $filePath
fileSize = $file.Length
hashCode = $fileHash
}
$obj.files += $fileObj
}
}
else
{
"no files found" >> $manifestPath
}

}
$json = $Obj | ConvertTo-Json
$json >> $manifestPath

}


JSON file

{
"files": [
{
"frameworkVersion": "4.0.73",
"files": [
{
"fileSize": 3863,
"fileName": ".\\app.config",
"hashCode": "7638235ea4b352f3843cef18be3a3dc2"
},
{
"fileSize": 273408,
"fileName": ".\\AutoMapper.dll",
"hashCode": "a1eb27bb54abacecff4305e095600ff3"
},
{
"fileSize": 110456,
"fileName": ".\\AutoMapper.xml",
"hashCode": "d7b7bd30045a09d4074635b82b454ad2"
},
{
"fileSize": 517928,
"fileName": ".\\DevExpress.Charts.v15.1.Core.dll",
"hashCode": "54df68a0597597d59ef8f6785e38cecf"
},
{
"fileSize": 3697448,
"fileName": ".\\DevExpress.Data.v15.1.dll",
"hashCode": "52a6b8d0a244adc0bcded11de3cb0f01"
},
{
"fileSize": 369448,
"fileName": ".\\DevExpress.Mvvm.v15.1.dll",
"hashCode": "315587898dcb938b55cc3e33e0f641b8"
},
{
"fileSize": 811816,
"fileName": ".\\DevExpress.Office.v15.1.Core.dll",
"hashCode": "4f281ea57cdf33e4b2869dcbf513a1eb"
},
{
"fileSize": 1780008,
"fileName": ".\\DevExpress.PivotGrid.v15.1.Core.dll",
"hashCode": "af5490679164c50065b880b05ef6f36a"
},
{
"fileSize": 3480872,
"fileName": ".\\DevExpress.Printing.v15.1.Core.dll",
"hashCode": "1d501f60bdbbbfa3ae7cf62375d19bb7"
},
{
"fileSize": 6057768,
"fileName": ".\\DevExpress.RichEdit.v15.1.Core.dll",
"hashCode": "892793249ca1548089e180dcb897cc55"
},
{
"fileSize": 2008872,
"fileName": ".\\DevExpress.Xpf.Charts.v15.1.dll",
"hashCode": "581d092a560f7e988c576702c00a066c"
},
{
"fileSize": 7893288,
"fileName": ".\\DevExpress.Xpf.Core.v15.1.dll",
"hashCode": "706a5eeb1679dcf0097ba0a87058f1a4"
},
{
"fileSize": 1428776,
"fileName": ".\\DevExpress.Xpf.Docking.v15.1.dll",
"hashCode": "e4f2be4b411c78b8d413bc18b12c4943"
},
{
"fileSize": 323368,
"fileName": ".\\DevExpress.Xpf.DocumentViewer.v15.1.Core.dll",
"hashCode": "ef07c34f55fa1274b77a628a6c10f1ad"
},
{
"fileSize": 1578792,
"fileName": ".\\DevExpress.Xpf.Gauges.v15.1.dll",
"hashCode": "a2a8e6ae5e8dd53f1d054a7e7d553885"
},
{
"fileSize": 1277736,
"fileName": ".\\DevExpress.Xpf.Grid.v15.1.Core.dll",
"hashCode": "83a0ac14fcdefa041ceecad2df44d484"
},
{
"fileSize": 1527592,
"fileName": ".\\DevExpress.Xpf.Grid.v15.1.dll",
"hashCode": "9d898230c3f44f629a15409aea5563a7"
},
{
"fileSize": 150312,
"fileName": ".\\DevExpress.Xpf.Grid.v15.1.Extensions.dll",
"hashCode": "3f8800eeb3824d33fa428d49100ff7dc"
},
{
"fileSize": 105768,
"fileName": ".\\DevExpress.Xpf.Layout.v15.1.Core.dll",
"hashCode": "4b0d036bb1ca24094a3aefacff06dac5"
},
{
"fileSize": 453416,
"fileName": ".\\DevExpress.Xpf.LayoutControl.v15.1.dll",
"hashCode": "256bddea67ee489b57b129149294d55c"
},
{
"fileSize": 1021736,
"fileName": ".\\DevExpress.Xpf.PivotGrid.v15.1.dll",
"hashCode": "cffdd1d6aedd64c4bd54e8f929f41312"
},
{
"fileSize": 923944,
"fileName": ".\\DevExpress.Xpf.Printing.v15.1.dll",
"hashCode": "8e4bb641617509d0a031c70fe4d34c9d"
},
{
"fileSize": 1564456,
"fileName": ".\\DevExpress.Xpf.Ribbon.v15.1.dll",
"hashCode": "4af47f8b9955d16d7c6a40979b852a7f"
},
{
"fileSize": 3148584,
"fileName": ".\\DevExpress.Xpf.Themes.MetropolisDark.v15.1.dll",
"hashCode": "b9788902caed807e764cd5e4f7221834"
},
{
"fileSize": 61952,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Analytics.dll",
"hashCode": "e4e35bc4f5a5e01ff0ea5da4f0bb51ee"
},
{
"fileSize": 99840,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Analytics.pdb",
"hashCode": "7af3b25119dd613ac7036d11c14723c0"
},
{
"fileSize": 12288,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.BusinessLogic.dll",
"hashCode": "807637828c899af266b2c0bc7eb51e40"
},
{
"fileSize": 32256,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.BusinessLogic.pdb",
"hashCode": "082695da11b1a87e667201f2d6e38d1e"
},
{
"fileSize": 9728,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.BusinessLogic.Tests.dll",
"hashCode": "b8e64c048b1dfcabe2175992d1d703b8"
},
{
"fileSize": 3863,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.BusinessLogic.Tests.dll.config",
"hashCode": "7638235ea4b352f3843cef18be3a3dc2"
},
{
"fileSize": 17920,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.BusinessLogic.Tests.pdb",
"hashCode": "963589b3297526a3aa257dd9972cc2cd"
},
{
"fileSize": 28160,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.dll",
"hashCode": "bb8e1aa15ea65ef416dbc95f6b266d7a"
},
{
"fileSize": 504,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.dll.config",
"hashCode": "11143b2440633f8761d500f91fde22e1"
},
{
"fileSize": 52736,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.pdb",
"hashCode": "b8578dee73f5ed3310f292e757ffcb07"
},
{
"fileSize": 10752,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.Common.TestUtils.dll",
"hashCode": "465d9beb1105b43a4420cf69077792e2"
},
{
"fileSize": 1020,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.Common.TestUtils.dll.config",
"hashCode": "31fc7691d14f201c568f83d289400cde"
},
{
"fileSize": 22016,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.Common.TestUtils.pdb",
"hashCode": "f5f52a23b19de914421d21e37b6289e2"
},
{
"fileSize": 1145856,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.UI.dll",
"hashCode": "1a60a0c53396cb3755006fedd399df4c"
},
{
"fileSize": 1026,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.UI.dll.config",
"hashCode": "fdcea35572295b7f5d073042c0b09094"
},
{
"fileSize": 384512,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.UI.pdb",
"hashCode": "eaabaa9cba67a0328b0669d3613b11a6"
},
{
"fileSize": 101888,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.ViewModels.dll",
"hashCode": "f1154a8b5cf5606109252fb55f84cb1c"
},
{
"fileSize": 1014,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.ViewModels.dll.config",
"hashCode": "51da0e4f9f4ef7ebce39a09eebb351ab"
},
{
"fileSize": 226816,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.ViewModels.pdb",
"hashCode": "ccbaabbfc4c6dd6d3870e98f5516a977"
},
{
"fileSize": 260096,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.Views.dll",
"hashCode": "5d92c6822e8a23d7c689e9afa97fa6f6"
},
{
"fileSize": 1020,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.Views.dll.config",
"hashCode": "31fc7691d14f201c568f83d289400cde"
},
{
"fileSize": 486912,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Common.Views.pdb",
"hashCode": "0b944e8f5f43b6a4fb71659a31ee27f7"
},
{
"fileSize": 124928,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Entity.dll",
"hashCode": "410b251eddecf40362a731e61f225671"
},
{
"fileSize": 140800,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Entity.pdb",
"hashCode": "800e2565f1c2d5b5ae2185b4994aa8db"
},
{
"fileSize": 16450048,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.MockData.dll",
"hashCode": "b4f5b2f59667a07468452dd4f9ce0b56"
},
{
"fileSize": 763,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.MockData.dll.config",
"hashCode": "a1cf7992c426410633d7e00a49c4ab31"
},
{
"fileSize": 89600,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.MockData.pdb",
"hashCode": "839d94600e446f86d1e9ab89d06811bc"
},
{
"fileSize": 140800,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Models.dll",
"hashCode": "85a5bd2b06905c45dda6b74c354b1f6d"
},
{
"fileSize": 13824,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Models.pdb",
"hashCode": "a603ac8073226553b347f5837d56c100"
},
{
"fileSize": 2102784,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.PmToolkit.dll",
"hashCode": "6a3838e12d7f7bea67a1f7b75e4fbb4c"
},
{
"fileSize": 1020,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.PmToolkit.dll.config",
"hashCode": "ef1a2f747c4df9853a175e181af62459"
},
{
"fileSize": 3974656,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.PmToolkit.pdb",
"hashCode": "9ebe6d3265bf865b7a2b8f24ec7c100a"
},
{
"fileSize": 112640,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.PMToolkit.Tests.dll",
"hashCode": "cdf0b89d7eb01c33b913973b7b72d522"
},
{
"fileSize": 3863,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.PMToolkit.Tests.dll.config",
"hashCode": "7638235ea4b352f3843cef18be3a3dc2"
},
{
"fileSize": 210432,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.PMT.PMToolkit.Tests.pdb",
"hashCode": "7c3a22df7e6c0755f1ff7009fbe1c0ce"
},
{
"fileSize": 169984,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Repository.dll",
"hashCode": "944d9a76212fa61834f31124dccfc914"
},
{
"fileSize": 757,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Repository.dll.config",
"hashCode": "aaed095788fab055449c48ea5df49259"
},
{
"fileSize": 288256,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Repository.pdb",
"hashCode": "10d00e3fffcd39983600b6c01aa4d34f"
},
{
"fileSize": 68608,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Services.dll",
"hashCode": "f8e3d3838e07983ff7127915150cd236"
},
{
"fileSize": 763,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Services.dll.config",
"hashCode": "a1cf7992c426410633d7e00a49c4ab31"
},
{
"fileSize": 91648,
"fileName": ".\\Jpmc.Am.Grt.Gims.Mas.Pmt.Services.pdb",
"hashCode": "cb7c6b60e3e26e983d2002e6a8b48d6a"
},
{
"fileSize": 376320,
"fileName": ".\\Jpmc.Am.Grt.Hibiscus.Dynamic.v15_1.pdb",
"hashCode": "b6a3262ed51c751f16e052b60f274fc2"
},
{
"fileSize": 116224,
"fileName": ".\\Jpmc.Am.Grt.Hibiscus.Themes.v15_1.pdb",
"hashCode": "42287b33b0915d339f8a1481c7ef91bd"
},
{
"fileSize": 269312,
"fileName": ".\\Jpmc.QT.Gmag.Common.dll",
"hashCode": "dfd5acaa9433eab7a67b38cc3b5d6f26"
},
{
"fileSize": 130560,
"fileName": ".\\Jpmc.QT.Shared.dll",
"hashCode": "37c14eb6cb21e584ce9bbcbb56f3dec3"
},
{
"fileSize": 6,
"fileName": ".\\lob.config",
"hashCode": "07ebd88009ccc51d295c73b615f01f28"
},
{
"fileSize": 288768,
"fileName": ".\\log4net.dll",
"hashCode": "179e7321f8bacc32b2bbac8cf02613ca"
},
{
"fileSize": 1438186,
"fileName": ".\\log4net.xml",
"hashCode": "cae1ac02326badf167079d6ad96ecef5"
},
{
"fileSize": 91648,
"fileName": ".\\Microsoft.Expression.Interactions.dll",
"hashCode": "801ae2682bf8bd7eea229ae01ff7cf42"
},
{
"fileSize": 75468,
"fileName": ".\\Microsoft.Expression.Interactions.xml",
"hashCode": "948616893339868ee70515c43d9e0a25"
},
{
"fileSize": 153416,
"fileName": ".\\Microsoft.Practices.Prism.dll",
"hashCode": "1488c726a98f80c3ddc6a1d00a8d0b8b"
},
{
"fileSize": 124216,
"fileName": ".\\Microsoft.Practices.Unity.dll",
"hashCode": "8bb973283d503c9dee003476810018c1"
},
{
"fileSize": 521216,
"fileName": ".\\Newtonsoft.Json.dll",
"hashCode": "bd8dd0829bc3dd28ae16dd34ab9a2c19"
},
{
"fileSize": 514743,
"fileName": ".\\Newtonsoft.Json.xml",
"hashCode": "54380313546c98dc96bd078a656f2275"
},
{
"fileSize": 586240,
"fileName": ".\\NSubstitute.dll",
"hashCode": "8f38627ad235c93bb3f55b772688f272"
},
{
"fileSize": 35848,
"fileName": ".\\NSubstitute.xml",
"hashCode": "ee744b30d738cec4752d23b44d5504a9"
},
{
"fileSize": 151552,
"fileName": ".\\nunit.framework.dll",
"hashCode": "4454f896213433f0bf889074375b92c8"
},
{
"fileSize": 583343,
"fileName": ".\\nunit.framework.xml",
"hashCode": "b2f25c46646c7a74fe6a65b1cada2843"
},
{
"fileSize": 2475,
"fileName": ".\\PMT.PMToolkit.Dev.dll.config",
"hashCode": "4e64a022868c451007e6b220dedf45e7"
},
{
"fileSize": 2211,
"fileName": ".\\PMT.PMToolkit.Research.dll.config",
"hashCode": "372f627e48acc6faf5ab3ecdafd13fff"
},
{
"fileSize": 2211,
"fileName": ".\\PMT.PMToolkit.Test.dll.config",
"hashCode": "3317aeffee9e4dd9bae1dd120376757f"
},
{
"fileSize": 200440,
"fileName": ".\\System.Collections.Immutable.dll",
"hashCode": "c519584b459c4491a150ec59979107fb"
},
{
"fileSize": 776808,
"fileName": ".\\System.Collections.Immutable.xml",
"hashCode": "c585777b4044d929a69e50d1ba6390d6"
},
{
"fileSize": 191152,
"fileName": ".\\System.Net.Http.dll",
"hashCode": "3002e884c5c15a15b68eaef3c62ff254"
},
{
"fileSize": 115425,
"fileName": ".\\System.Net.Http.xml",
"hashCode": "2bf947b4b9995c61aa8a427644f602d0"
},
{
"fileSize": 104648,
"fileName": ".\\System.Reactive.Core.dll",
"hashCode": "f3f361cf367660054a96a7757d530a8f"
},
{
"fileSize": 174535,
"fileName": ".\\System.Reactive.Core.xml",
"hashCode": "fbcbf5de95fcc69db290a85a26329aea"
},
{
"fileSize": 24264,
"fileName": ".\\System.Reactive.Interfaces.dll",
"hashCode": "fecbd4b8b3cba96244b455ad22779b3d"
},
{
"fileSize": 20236,
"fileName": ".\\System.Reactive.Interfaces.xml",
"hashCode": "6e0c6eea2a37cec0c868dca0b370fb18"
},
{
"fileSize": 704200,
"fileName": ".\\System.Reactive.Linq.dll",
"hashCode": "b5f9de367187476d737ce60f2112550f"
},
{
"fileSize": 1015578,
"fileName": ".\\System.Reactive.Linq.xml",
"hashCode": "371f39ede66864e07136c48b2bff6cf0"
},
{
"fileSize": 38088,
"fileName": ".\\System.Reactive.PlatformServices.dll",
"hashCode": "b64acef473237ae1bd252b4752ac7b39"
},
{
"fileSize": 24749,
"fileName": ".\\System.Reactive.PlatformServices.xml",
"hashCode": "d3b77756b4ee9e7eada0d1d07233aa3f"
},
{
"fileSize": 28872,
"fileName": ".\\System.Reactive.Windows.Threading.dll",
"hashCode": "29d23726395ae2a0d1aae4e38c71e02c"
},
{
"fileSize": 15191,
"fileName": ".\\System.Reactive.Windows.Threading.xml",
"hashCode": "baf5792f2945a2b079189f1f4d5729d9"
},
{
"fileSize": 39936,
"fileName": ".\\System.Windows.Interactivity.dll",
"hashCode": "ca609bf1f4b5553e7c5103e9efaf0ca0"
},
{
"fileSize": 61056,
"fileName": ".\\System.Windows.Interactivity.xml",
"hashCode": "1087ecd40eb8e4e21a4530111658dba7"
},
{
"fileSize": 467,
"fileName": ".\\Config\\PMT.PortfolioManagementToolkit.Config.Json",
"hashCode": "e5cb5262948398a2cac4137adf8ebb63"
}
],
"name": "MAS-PMT",
"version": "2.0.12"
}

Answer

Your JSON has several problems.

The first issue is not very apparent based on the error from JSONLint, but it's because the value of the fileName key contains an unescaped backslash \ so it's interpreting it as an unrecognized escape sequence. Those need to be changed to \\.

Once you fix that, you'll realize that there's no comma , separating the fileSize and the hashCode keys, and that the hash codes themselves need to be quoted too since they are strings.

If you're generating this JSON with string concatenation, you should stop that and create an object within PowerShell, and then send it to ConvertTo-Json. Something like this perhaps (untested):

$obj = @{
    name = "Project name"
    version = "2.0.8"
    frameworkVersion = "4.0.73"
    files = @() 
}

Get-ChildItem -Path C:\my\Path -Recurse | ForEach-Object {
    $fileObj = @{
        fileName = $_.Name
        fileSize = $_.Size
        hashCode = Get-MyHashCode $_
    }
    $obj.files += $fileObj
}

$json = $myObj | ConvertTo-Json