You can split the input text in rows and then ‘JSON.parse’ the single row.
let myObj = this.responseText.split('n')
.filter(line => line !== "")
.map(JSON.parse);
Example based on your string:
let text = `{"ProgMode":"on","wait_h":"5","wait_m":"5","output":"1"}
{"ProgMode":"off","wait_h":"10","wait_m":"10","output":"2"}
`
let myObj = text.split('n')
.filter(line => line !== "")
.map(JSON.parse);
console.log(myObj)
Solution 2 :
you don’t need to split and map, it is to heavy for this case, try this
var myObj = JSON.parse(`[${this.responseText.replaceAll("}n","},")}]`);
this is a little more complicated, but much more reliable,since it is not using any special symbols
var myObj = JSON.parse(`[{${this.responseText.substring(1).replaceAll("{",",{")}]`);
Problem :
I can’t change json format. The data store in new line.
json file:
return this error: Uncaught SyntaxError: Unexpected end of JSON input
Comment posted by mplungjan
@MSaberi the code works on your example string
Comment posted by M Saberi
3rd party devices fill json file with extra enter (/n) end of file. when I remove it manually everything fine. any solution to remove automatically empty line from end of the file?
Comment posted by TheGr8_Nik
@MSaberi updated the response, you can use filter to ignore empty lines
Comment posted by M Saberi
return this error: SyntaxError: Unexpected token { in JSON at position 120
Comment posted by Serge
@MSaberi It is working properly on my mashine, but maybe your json file is different from json you posted. Try this var myObj = JSON.parse(
Comment posted by Serge
@MSaberi Or you can try my updated answer if it is still not working
Comment posted by M Saberi
thanks. your latest update worked fine. and the advantage is work with any empty lines.