I have a text file with multiple JSON objects separated by \n. How can I read it in using TypeScript?
I am trying something like this:
const text = await readFileAsText(files[0])
const lines = text?.split('\n')
console.log('DEBUG ', { lines, text })
const cityjson = JSON.parse(lines[0]) as CityJSON
const cityjsonfeatures = lines
.slice(1)
.map((line) => JSON.parse(line) as CityJSONFeature)
which does not work, because it parses the line as a complete string like so:
JSON.parse(lines[0]) fails with:
CityJsonForm.tsx:191 SyntaxError: Unexpected end of JSON input
How can I make it recognize the string as a JSON object?
EDIT: here is an example code to replicate the problem:
const text = `{"type":"CityJSON","version":"2.0","CityObjects":{},"vertices":[],"transform":{"scale":[0.001,0.001,0.001],"translate":[78248.66,457604.591,2.463]},"metadata":{"geographicalExtent":[78248.66,457604.591,2.463,79036.024,458276.439,37.481],"referenceSystem":"https://www.opengis.net/def/crs/EPSG/0/7415"}}
{"type":"CityJSONFeature","CityObjects":{"GUID_47061B3E-C490-4882-806A-EC084006045F_1":{"type":"BuildingPart","attributes":{"roofType":"1030","RelativeEavesHeight":6.506,"RelativeRidgeHeight":9.692,"AbsoluteEavesHeight":12.104,"AbsoluteRidgeHeight":15.29},"geometry":[{"type":"Solid","boundaries":[[[[0,1,2,3]],[[1,4,5,2]],[[4,6,7,5]],[[6,0,3,7]],[[4,1,8]],[[0,6,9]],[[1,0,9,8]],[[6,4,8,9]],[[3,2,5,7]]]],"semantics":{"values":[[0,1,2,3,4,5,6,7,8]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":55.81,"Slope":56.079},{"type":"RoofSurface","Direction":235.778,"Slope":56.079},{"type":"GroundSurface"}]},"material":{"":{"values":[[0,0,0,0,0,0,1,1,2]]}},"lod":"2"}],"parents":["GUID_47061B3E-C490-4882-806A-EC084006045F"]},"GUID_47061B3E-C490-4882-806A-EC084006045F_2":{"type":"BuildingPart","attributes":{"roofType":"1000","RelativeEavesHeight":2.613,"RelativeRidgeHeight":2.613,"AbsoluteEavesHeight":8.663,"AbsoluteRidgeHeight":8.663},"geometry":[{"type":"Solid","boundaries":[[[[10,11,12,13]],[[11,14,15,12]],[[14,16,17,15]],[[16,10,13,17]],[[16,14,11,10]],[[13,12,15,17]]]],"semantics":{"values":[[0,1,2,3,4,5]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":0,"Slope":90},{"type":"GroundSurface"}]},"material":{"":{"values":[[3,3,3,3,4,5]]}},"lod":"2"}],"parents":["GUID_47061B3E-C490-4882-806A-EC084006045F"]},"GUID_47061B3E-C490-4882-806A-EC084006045F":{"type":"Building","children":["GUID_47061B3E-C490-4882-806A-EC084006045F_1","GUID_47061B3E-C490-4882-806A-EC084006045F_2"]}},"vertices":[[329976,339888,9640],[332644,335961,9640],[332644,335961,3135],[329976,339888,3135],[324815,330627,9640],[324815,330627,3135],[322120,334586,9640],[322120,334586,3135],[328729,333293,12826],[326048,337238,12826],[324815,330627,6200],[322872,329304,6200],[322872,329304,3586],[324815,330627,3586],[320361,333098,6200],[320361,333098,3586],[322267,334369,6200],[322267,334369,3586]],"appearance":{"materials":[{"name":"UUID_575229dc-663b-43ee-bbe8-9b09a366cefc","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_2069a30e-6881-4698-bf46-e66d30219634","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_8f7d0789-ec3a-4ef5-ac9b-de643d37ab59","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]},{"name":"UUID_afb1e01b-3e14-47c0-b234-548bd97240c1","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_c9fe1f1c-cc3a-40f4-980d-59b698fa24a8","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_5bb5536c-51b5-4f1c-85ba-47784f728d06","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]}]},"id":"GUID_47061B3E-C490-4882-806A-EC084006045F"}
{"type":"CityJSONFeature","CityObjects":{"GUID_3D7D60B9-8F3A-4D3B-A3E5-CD9B5565A5B2":{"type":"Building","attributes":{"roofType":"1030","RelativeEavesHeight":6.148,"RelativeRidgeHeight":9.508,"AbsoluteEavesHeight":11.38,"AbsoluteRidgeHeight":14.739},"geometry":[{"type":"Solid","boundaries":[[[[0,1,2,3]],[[1,4,5,2]],[[4,6,7,5]],[[6,0,3,7]],[[4,1,8]],[[0,6,9]],[[1,0,9,8]],[[6,4,8,9]],[[3,2,5,7]]]],"semantics":{"values":[[0,1,2,3,4,5,6,7,8]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":130.846,"Slope":53.978},{"type":"RoofSurface","Direction":310.986,"Slope":53.979},{"type":"GroundSurface"}]},"material":{"":{"values":[[0,0,0,0,0,0,1,1,2]]}},"lod":"2"}]}},"vertices":[[392125,306049,8917],[388476,301820,8917],[388476,301820,2769],[392125,306049,2769],[381498,307898,8917],[381498,307898,2769],[385205,312154,8917],[385205,312154,2769],[384987,304859,12276],[388665,309103,12276]],"appearance":{"materials":[{"name":"UUID_372db6e9-c842-49af-85e7-5ef90f146ca4","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_9482f59e-5b25-4bf0-b00f-a2d0fd7296ce","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_04d4d206-f1e3-4a01-b897-a2d1574dbd3b","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]}]},"id":"GUID_3D7D60B9-8F3A-4D3B-A3E5-CD9B5565A5B2"}
{"type":"CityJSONFeature","CityObjects":{"GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_2":{"type":"BuildingPart","attributes":{"roofType":"1000","RelativeEavesHeight":3.913,"RelativeRidgeHeight":3.913,"AbsoluteEavesHeight":7.635,"AbsoluteRidgeHeight":7.635},"geometry":[{"type":"Solid","boundaries":[[[[0,1,2,3]],[[1,4,5,2]],[[4,6,7,5]],[[6,0,3,7]],[[6,4,1,0]],[[3,2,5,7]]]],"semantics":{"values":[[0,1,2,3,4,5]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":0,"Slope":90},{"type":"GroundSurface"}]},"material":{"":{"values":[[0,0,0,0,1,2]]}},"lod":"2"}],"parents":["GUID_F784906B-FD5A-4F81-BACF-79C3566B2789"]},"GUID_F784906B-FD5A-4F81-BACF-79C3566B2789":{"type":"Building","children":["GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_1","GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_2","GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_3"]},"GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_1":{"type":"BuildingPart","attributes":{"roofType":"1000","RelativeEavesHeight":5.012,"RelativeRidgeHeight":5.012,"AbsoluteEavesHeight":8.521,"AbsoluteRidgeHeight":8.521},"geometry":[{"type":"Solid","boundaries":[[[[8,9,10,11]],[[9,12,13,10]],[[12,14,15,13]],[[14,16,17,15]],[[16,18,19,17]],[[18,20,21,19]],[[20,8,11,21]],[[20,18,16,14,12,9,8]],[[11,10,13,15,17,19,21]]]],"semantics":{"values":[[0,1,2,3,4,5,6,7,8]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":0,"Slope":90},{"type":"GroundSurface"}]},"material":{"":{"values":[[3,3,3,3,3,3,3,4,5]]}},"lod":"2"}],"parents":["GUID_F784906B-FD5A-4F81-BACF-79C3566B2789"]},"GUID_F784906B-FD5A-4F81-BACF-79C3566B2789_3":{"type":"BuildingPart","attributes":{"roofType":"1000","RelativeEavesHeight":3.929,"RelativeRidgeHeight":3.929,"AbsoluteEavesHeight":8.035,"AbsoluteRidgeHeight":8.035},"geometry":[{"type":"Solid","boundaries":[[[[22,23,24,25]],[[23,26,27,24]],[[26,28,29,27]],[[28,30,31,29]],[[30,22,25,31]],[[30,28,26,23,22]],[[25,24,27,29,31]]]],"semantics":{"values":[[0,1,2,3,4,5,6]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":0,"Slope":90},{"type":"GroundSurface"}]},"material":{"":{"values":[[6,6,6,6,6,7,8]]}},"lod":"2"}],"parents":["GUID_F784906B-FD5A-4F81-BACF-79C3566B2789"]}},"vertices":[[526322,376230,5172],[528698,373119,5172],[528698,373119,1259],[526322,376230,1259],[524319,369887,5172],[524319,369887,1259],[521993,373073,5172],[521993,373073,1259],[511989,365777,6058],[508266,370827,6058],[508266,370827,1045],[511989,365777,1045],[525201,383282,6058],[525201,383282,1045],[527171,380478,6058],[527171,380478,1045],[526489,379988,6058],[526489,379988,1045],[528271,377804,6058],[528271,377804,1045],[526322,376230,6058],[526322,376230,1045],[524319,369887,5572],[525135,368723,5572],[525135,368723,1642],[524319,369887,1642],[515163,361548,5572],[515163,361548,1642],[511989,365777,5572],[511989,365777,1642],[521993,373073,5572],[521993,373073,1642]],"appearance":{"materials":[{"name":"UUID_af967a83-c8ad-41b2-966a-bd19ae4fb35e","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_2af0ac25-c5ce-42ba-938e-cb566d95b815","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_588717aa-5c37-4c57-abed-1f8c04aa5787","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]},{"name":"UUID_af3f5f86-f506-478b-9eaa-1feaead041e9","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_cba6e3dd-db29-4cda-a158-de2203556d6e","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_f9bd12fe-3174-4a70-9fe9-2b2b9acfb132","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]},{"name":"UUID_706d343d-94d5-40f7-808d-f6388c14fa69","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_929af31f-5e91-419c-9f55-28b5ba63009f","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_16223f77-3a15-42d4-bfd0-95ba100ac4b0","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]}]},"id":"GUID_F784906B-FD5A-4F81-BACF-79C3566B2789"}
{"type":"CityJSONFeature","CityObjects":{"GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70":{"type":"Building","children":["GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70_1","GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70_2"]},"GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70_2":{"type":"BuildingPart","attributes":{"roofType":"1030","RelativeEavesHeight":5.43,"RelativeRidgeHeight":8.787,"AbsoluteEavesHeight":9.893,"AbsoluteRidgeHeight":13.25},"geometry":[{"type":"Solid","boundaries":[[[[0,1,2,3]],[[1,4,5,2]],[[4,6,7,5]],[[6,0,3,7]],[[4,1,8]],[[0,6,9]],[[1,0,9,8]],[[6,4,8,9]],[[3,2,5,7]]]],"semantics":{"values":[[0,1,2,3,4,5,6,7,8]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":225.306,"Slope":55.008},{"type":"RoofSurface","Direction":44.363,"Slope":55.003},{"type":"GroundSurface"}]},"material":{"":{"values":[[0,0,0,0,0,0,1,1,2]]}},"lod":"2"}],"parents":["GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70"]},"GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70_1":{"type":"BuildingPart","attributes":{"roofType":"1010","RelativeEavesHeight":2.973,"RelativeRidgeHeight":2.973,"AbsoluteEavesHeight":7.455,"AbsoluteRidgeHeight":7.455},"geometry":[{"type":"Solid","boundaries":[[[[10,11,12,13]],[[11,14,15,12]],[[14,16,17,15]],[[16,10,13,17]],[[16,14,11,10]],[[13,12,15,17]]]],"semantics":{"values":[[0,1,2,3,4,5]],"surfaces":[{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"WallSurface"},{"type":"RoofSurface","Direction":0,"Slope":90},{"type":"GroundSurface"}]},"material":{"":{"values":[[3,3,3,3,4,5]]}},"lod":"2"}],"parents":["GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70"]}},"vertices":[[386129,400546,7430],[382667,404106,7430],[382667,404106,2000],[386129,400546,2000],[389410,410810,7430],[389410,410810,2000],[393067,407291,7430],[393067,407291,2000],[386038,407458,10786],[389599,403918,10786],[390869,409407,4992],[391152,409679,4992],[391152,409679,2019],[390869,409407,2019],[393323,407539,4992],[393323,407539,2019],[393067,407291,4992],[393067,407291,2019]],"appearance":{"materials":[{"name":"UUID_9721fe63-453b-4c0e-bcc7-12cf3e701e92","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_bc679048-44a0-42c0-a5e7-ec07477876e2","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_c1cf0b04-ebb2-4d73-888b-5a134b16207d","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]},{"name":"UUID_7f62a204-9449-47e2-afc2-1133df337567","ambientIntensity":0.2,"diffuseColor":[0.922,0.922,0.922]},{"name":"UUID_ee8b61dd-96b4-4574-b05b-d25454929ccb","ambientIntensity":0.2,"diffuseColor":[0.502,0,0]},{"name":"UUID_401725b5-30d8-4186-b32e-c6df04e8f4cb","ambientIntensity":0.2,"diffuseColor":[0.502,0.502,0.502]}]},"id":"GUID_DCAE0ACB-5F38-4AEA-80D2-7242B193AE70"}
`
console.log('DEBUG TEXT', text)
const lines = text?.split('\n')
console.log('DEBUG LINES', lines)
// this is not a JSON as it should be, here it is only a string in a Json
const parsed = JSON.stringify(lines[0])
console.log('DEBUG PARSED ', { lines, text, parsed })

.split("\n")method to split the string file in array thenJSON.parseevery json in the array[, and add a,after each line, and finally add a]to the end, making the complete text file an array of JSON objects, and then use Split JSON array into separate files/objects