0

I have objects I'm trying to extract from a HTML file.

<h1> heading</h1>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
{"data" : {"here" : "test"} }
<h2> heading</h2>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
{"data2" : {"here" : "test"} }

How would I go about extracting the Objects so I can then parse them with JSON.parse() and use them in my JS code?

Essentially stripping anything that is not an object from the text / html file.

{"data" : {"here" : "test"} }
{"data2" : {"here" : "test"} }
1
  • 2
    First question is what source would mix json as text nodes with html like that? Would make more sense if it was mixed into data attributes or <code> or <pre> tags but not the way it is shown Commented Jul 16, 2019 at 15:13

2 Answers 2

4

You can loop over the nodes and select the text nodes.

var objs = Array.from(document.body.childNodes) // select all the nodes
  .filter( // pick out just the lines we want
    node => 
      node.nodeType === Node.TEXT_NODE &&  // text node
      node.nodeValue.trim().length  // get rid of line feeds
  ).map(node =>
    JSON.parse(node.nodeValue.trim())) // convert to object

console.log(objs)
<h1> heading</h1>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
{"data" : {"here" : "test"} }
<h2> heading</h2>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
<p> dsfhklsd sdlfjklsdj ljsdkflj</p>
{"data2" : {"here" : "test"} }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!
1

Assuming that your source is being processed by JavaScript as a string, I would use a Regex delete all the html elements (assuming that the JSON you want isn't wrapped in an html element, like in your example) or parse everything between the curly brackets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.