Reputation: 100010
Some logging libraries write JSON strings to files and then parse them. I am writing a library for integration tests and want to know what the best way is to split the JSON strings so that I can create an array of the strings and then parse them one at a time.
This would be done like so:
var path = require('path');
var filePath = path.resolve(appRootPath + '/' + 'test/output/test1.txt');
var rstream = fs.createReadStream(filePath);
var data = '';
rstream
.on('data', function (chunk) {
data += chunk;
})
.on('end', function () { // done
var array = String(dataLength).split(';'); //spit the big string into an array of strings by some token
doTheThing(array.filter(function (item) { //filter and parse the array element by element
return item && item.length > 0;
}).map(function (item) {
return JSON.parse(String(item));
}));
});
Below, I just put in semi-colons as a delineating character, however that won't be good enough for the obvious reasons.
So I am looking to parse something like this big string below (forgive the nonsense words like 'dogs' and 'cats'):
{"testId":21,"desc":"cats 1"};{"testId":18,"desc":"dogs 1"};{"testId":1,"desc":"makes stuff 1","error":{}};{"testId":22,"desc":"cats 2"};{"testId":19,"desc":"dogs 2"};{"testId":2,"desc":"makes stuff 2","error":{}};{"testId":20,"tests":[{"testId":21,"type":"it-standard","desc":"cats 1","complete":false,"error":null},{"testId":22,"type":"it-standard","desc":"cats 2","complete":false,"error":null}],"testsParallel":[],"loopTests":[],"children":[],"error":null};{"testId":17,"tests":[{"testId":18,"type":"it-standard","desc":"dogs 1","complete":false,"error":null},{"testId":19,"type":"it-standard","desc":"dogs 2","complete":false,"error":null}],"testsParallel":[],"loopTests":[],"children":[{"testId":20}],"error":null};{"testId":3,"desc":"makes stuff 2","error":null};{"testId":4,"desc":"makes stuff 2","error":{}};{"testId":10,"desc":"makes stuff 1","error":{}};{"testId":11,"desc":"makes stuff 2","error":null};{"testId":12,"desc":"makes stuff 3","error":{}};{"testId":14,"desc":"makes stuff 44","error":{}};{"testId":15,"desc":"makes stuff 888","error":{}};{"testId":16,"desc":"makes stuff 999","error":{}};{"testId":6,"desc":"makes stuff chard","error":{}};{"testId":7,"desc":"makes stuff heeee","error":{}};{"testId":8,"desc":"makes stuff raaa","error":{}};{"testId":0,"tests":[{"testId":1,"type":"it-standard","desc":"makes stuff 1","complete":false,"error":null},{"testId":2,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null},{"testId":3,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null},{"testId":4,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null}],"testsParallel":[{"testId":9,"tests":[{"testId":10,"type":"it-parallel","desc":"makes stuff 1","complete":false,"error":null},{"testId":11,"type":"it-parallel","desc":"makes stuff 2","complete":false,"error":null},{"testId":12,"type":"it-parallel","desc":"makes stuff 3","complete":false,"error":null}],"type":"ParallelTestSet"},{"testId":13,"tests":[{"testId":14,"type":"it-parallel","desc":"makes stuff 44","complete":false,"error":null},{"testId":15,"type":"it-parallel","desc":"makes stuff 888","complete":false,"error":null},{"testId":16,"type":"it-parallel","desc":"makes stuff 999","complete":false,"error":null}],"type":"ParallelTestSet"}],"loopTests":[{"testId":5,"tests":[{"testId":6,"type":"it-loop","desc":"makes stuff chard","complete":false,"error":null},{"testId":7,"type":"it-loop","desc":"makes stuff heeee","complete":false,"error":null},{"testId":8,"type":"it-loop","desc":"makes stuff raaa","complete":false,"error":null}],"type":"LoopTestSet"}],"children":[{"testId":17}],"error":null};
I need a clever way to delineate the would-be JS objects with some special character that can never appear in the output (I can control what ends up in the output, but that might require that I filter the end user's data that ends up on disk).
How is this done? Is there some super special character or character sequence I can use? Or is there a 'cleverer' way to do it that I am not thinking of?
Upvotes: 0
Views: 2187
Reputation: 26
With JSON.parse() its possible to create an object out of a string. Maybe try it this way:
read the JSON file as a string
split the string into an array: string.split(';');
and now parse all the array entries in a loop with JSON.parse
Or just make an array in the JSON File and parse this complete thing
Upvotes: 1