I am trying to get a sample json file using fs.readFile and fs.read in nodejs. This is the json test file that I am reading
{
"HttpTestResponse":
[
{
"title":"testTitle2",
"id": 2,
"name":"testName3",
"testArray":[{"testProp1":"testPropVal2","testProp2":"testPropVal_2"}]
},
{
"title":"testTitle3",
"id": 3,
"name":"testName3",
"testArray":[{"testProp1":"testPropVal3","testProp2":"testPropVal_3"}]
}
]
}
This is the sample function (I am using typescript with nodejs for stricter typing)
getJSONFromFile(paramId) {
let obj: HttpTestModel[];
fs.exists(this.resolvedUrl, exists => {
if (exists) {
fs.readFile(this.resolvedUrl, 'utf-8', (err, data) => {
if (err) {
this.pino.info('error while reading the file', err);
} else {
this.pino.info('data', JSON.parse(data));
// tslint:disable-next-line:one-variable-per-declaration
// tslint:disable-next-line:prefer-const
// tslint:disable-next-line:one-variable-per-declaration
obj = JSON.parse(data);
this.pino.info('plain data', data);
this.pino.info('data in parseJSON', parseJson(data));
}
});
}
});
}
THis is the output that I get. I am using Pino module and hence the output has some default levels and timestamps
{"level":30,"time":1550563397320,"msg":"plain data {\r\n \"HttpTestResponse\":\r\n [\r\n
{ \r\n \"title\":\"testTitle2\",\r\n \"id\": 2,\r\n \"name\":\"testName3\",\r\n \"testArray\":[{\"testProp1\":\"testPropVal2\",\"testProp2\":\"testPropVal_2\"}]\r\n
},\r\n {\r\n \"title\":\"testTitle3\",\r\n \"id\": 3,\r\n \"name\":\"testName3\",\r\n \"testArray\":[{\"testProp1\":\"testPropVal3\",\"testProp2\":\"testPropVal_3\"}]\r\n }\r\n ]\r\n \r\n \r\n}","pid":21212,"hostname":"INDV072294","v":1}
{"level":30,"time":1550563397321,"msg":"stringifed data {\r\n \"HttpTestResponse\":\r\n [\r\n
{ \r\n \"title\":\"testTitle2\",\r\n \"id\": 2,\r\n \"name\":\"testName3\",\r\n \"testArray\":[{\"testProp1\":\"testPropVal2\",\"testProp2\":\"testPropVal_2\"}]\r\n },\r\n {\r\n \"title\":\"testTitle3\",\r\n \"id\": 3,\r\n
\"name\":\"testName3\",\r\n \"testArray\":[{\"testProp1\":\"testPropVal3\",\"testProp2\":\"testPropVal_3\"}]\r\n }\r\n ]\r\n \r\n \r\n}","pid":21212,"hostname":"INDV072294","v":1}
{"level":30,"time":1550563397322,"msg":"data in parseJSON {\"HttpTestResponse\":[{\"title\":\"testTitle2\",\"id\":2,\"name\":\"testName3\",\"testArray\":[{\"testProp1\":\"testPropVal2\",\"testProp2\":\"testPropVal_2\"}]},{\"title\":\"testTitle3\",\"id\":3,\"name\":\"testName3\",\"testArray\":[{\"testProp1\":\"testPropVal3\",\"testProp2\":\"testPropVal_3\"}]}]}","pid":21212,"hostname":"INDV072294","v":1}
I also tried to use the parse-json module but nothing is helpming me out with the output that I want. I would want the JSON object as output and not the string, so I could store that and iterate it and filter out the desired results
Could someone help me out on this?