html2json
html2json copied to clipboard
error when html contain any mark code
when the html contain some code
<!-- this is a test mark -->
node_modules/html2json/src/html2json.js:116 if (parent.child === undefined) { ^
TypeError: Cannot read property 'child' of undefined at Object.HTMLParser.comment (/mnt/d/workspace/peach2/wevue/node_modules/html2json/src/html2json.js:116:19) at HTMLParser.HTMLParser (/mnt/d/workspace/peach2/wevue/node_modules/html2json/lib/Pure-JavaScript-HTML5-Parser/htmlparser.js:126:16)
i got the same issue, any help?
you should remove DOCTYPE
and all the Comments
, then continue to convert it to JSON
, at the end if you desire to write it locally
const resp = response.replace(/\<(\?xml|(\!DOCTYPE[^\>\[]+(\[[^\]]+)?))+[^>]+\>/g, '');
const rs = resp.replace(/<\!--.*?-->/g, "");
const json = html2json(rs);
const data = JSON.stringify(json, null, 2)
var filename = `output-${Date.now()}.json`;
fs.writeFileSync(filename, data);