Home > Net >  Escaping json parsed from html using node-html-parser
Escaping json parsed from html using node-html-parser

Time:12-12

I'm trying to parse the application/ld json of a page parsed with node-html-parser I got it all working until I got this unescaped JSON issue where a \n in values is messing things up.

The small bit of JSON causing the issue (rest of JSON has been removed):

{
"name": "3. Given the balanced equation: 2H2(g)  
O2(g) --> 2H2O(l)
How many grams of H2O are formed if 9.00 mol H2(g) reacts
completely with an excess of O2(g)?
The molar mass of H2O is 18.0g/mol.
"
}

I tried using this escape function solution (simply put, str.replace(/[\n]/g, '\\n'), but it broke it.

How might I parse this string, with some values containing random new lines, and how to fix it?

Full Context (just for reference):

Source: https://www.numerade.com/ask/question/3-given-the-balanced-equation-2h2g-o2g-2h2ol-how-many-grams-of-h2o-are-formed-if-900-mol-h2g-reacts-completely-with-an-excess-of-o2g-the-molar-mass-of-h2o-is-180gmol-57997/

<script type="application/ld json">
  {
    "@context": "https://schema.org",
    "@type": "QAPage",
    "mainEntity": {
      "@type": "Question",
      "name": "3. Given the balanced equation: 2H2(g)  
O2(g) --&gt; 2H2O(l)
How many grams of H2O are formed if 9.00 mol H2(g) reacts
completely with an excess of O2(g)?
The molar mass of H2O is 18.0g/mol.
",
      "text": "3. Given the balanced equation: 2H2(g)  
O2(g) --&gt; 2H2O(l)
How many grams of H2O are formed if 9.00 mol H2(g) reacts
completely with an excess of O2(g)?
The molar mass of H2O is 18.0g/mol.
",
      "answerCount": 4,
      "dateCreated": "Oct. 9, 2021, 6:08 p.m.",
      "author": {
        "@type": "Person",
        
          "name": "Matthew J."
        
      },
      
        "acceptedAnswer": {
          "@type": "Answer",
          "upvoteCount": 3,
          "text": "In this problem, we have to find a mass of H2 that are formed if nine more of age to relax with an excess of so from balanced equation, we can see that two moles of is to Produced two moles of water, Then nine moles of H two must produce nine moles of water. So now we have most of water. We can easily find the mess MS. Of what, which is equally good number of multi multiply by the molar mass, so it is 1 62 g. So we can say that 1 62 g of H 20 are formed with nine more of age to react with Electricly with an excessive or two.",
          "dateCreated": "Oct. 13, 2021, 5:12 p.m.",
          "url": "https://www.numerade.com/ask/question/3-given-the-balanced-equation-2h2g-o2g-2h2ol-how-many-grams-of-h2o-are-formed-if-900-mol-h2g-reacts-completely-with-an-excess-of-o2g-the-molar-mass-of-h2o-is-180gmol-57997/",
          "author": {
            "@type": "Person",
            "name": "Taimoor Shabbir"
          }
        },
      
      "suggestedAnswer": [
        
            
            {
              "@type": "Answer",
              "text": "So we&#x27;re keeping the reaction between hydrogen all section when both off the gas, they ah ah, in the container. So a spark, An initial initiated reaction to your form. Water. So here we have soup on serial fees there from five graham off hydrogen and also syrup on Syria. When a five month off oxygen, what would be the mass of water being produce? So first of all, we have to Bannister can call the Ashram. Um, as you can see, that we have to Ah, hydrogen on the on the left. So we have it for two under, right? And then we have four hydrogen. So on the love we were just put put to you from the hydrogen and then the whole reaction Spartans. Okay, so the next step before we do anything is convert anything. That is not your number. Motion on verbal. So we&#x27;re still points several feet. That&#x27;s 75 year bites You where? Sierra 750.1 it if there were 18 eight. Uh, both. Okay, so, um, we have the lumber. I&#x27;m also had to found the limiting We agent we had found an emitting region, and then we can just a limbo move in different dimension. We agent to find out the mass off our water. Okay, so then they assume we Ah, I would pick all hydrogen. Assume I have. Ah, them. You have that much hydrogen, You know that? The footy we act so we will. How many oxygen do I need? The militia is 2 to 1 for Ah, hydrogen oxygen. So I just take ah, hydrogen. Um, the more motive I bite you, so I will have Ah, sirrah, point. Seriously. 09 for most of oxygen required to fully re at with. Ah, Hodgins. Okay, So this is the require amounts over here, over here. And there&#x27;s the actual mouth, so you can see that. Actually, we have a lot off oxygen. So the excess we aging essentially, it&#x27;s all sitting over here. All right, so let&#x27;s stab is we&#x27;re going to Ah, use hydrogen number mostly found that ward number almost again because we have enough also, June. But, uh, I know that the food that we have with all the hydrogen Okay, so the limbo move off water, it would be ah, we can find the from the motorway show and also find from the number of most of hydrogen. So the mother wisher is 2 to 2. Essential is 1 to 1. So it&#x27;s essentially the same as the number most over here for water. So sue syrup on Syria 18 Ah, for out to the moment of water. Okay, so when we know the number more water we have vowed a mass, we just the more plight, Um, the mass off the mill a massive waters with 18 by the limbo Moe&#x27;s so so far 018 times 18 And that which you have several 180.338 gram for water. Okay, so, uh, we&#x27;ve already filed the mass of water. We know that excess we agent sausage in. So how about the oxygen? We meaning? All right. So we have that much of all sojourn. 0.185 and then we know that you know that hopefully we have imagined they will consume syrup on sale soon. 94 So we just took our agent Noh Ah, concentration off our region. Know them. Don&#x27;t move for oxygen. Subtract Ah ah ah! Mom required if we were rehab before their hydrogen and that we should be able to find out is because the syrup on cereal night one most we meaning for oxygen remaining this we meaning. And then we can further, um, convert that back to our master. You corresponding to Syria 0.219 Grandma Oxygen. We may you know, we asked your picture.",
              "dateCreated": "Aug. 11, 2021, 12:50 a.m.",
              "upvoteCount": 3,
              "url": "https://www.numerade.com/questions/a-mixture-of-00375-g-of-hydrogen-and-00185-mol-oxygen-in-a-closed-container-is-sparked-to-initiate-a/",
              "author": {
                "@type": "Person",
                "name": "Stephen Ho"
              }
            },
            
        
            
            {
              "@type": "Answer",
              "text": "The reaction equation in this question is based on the same reaction equation that we had in the previous question. Now, during this reaction, five moles of hydrogen gas reacts with 0.15 moles of oxygen gas in order to produce a certain amount of water. We need to identify the limiting reactant here and also calculate the number of moles of water that can form during this reaction. For this purpose. We will look at two different situations in order to identify the limiting reactant first and that is, um firstly, we will look at the number of moles of water that can be produced If we start off with five moles of hydrogen gas, and secondly, we will look at the number of moles of water that can be produced when starting off with a 0.15 zero moles of oxygen gas. We will then compare these two situations in order to identify the limiting reactant. So, firstly, In order to determine the number of moles of water that can form when starting off with five miles of hydrogen gas, we need to work with the more ratio of water to hydrogen gas. So for this purpose, we will have a look at the stock geometric coefficients here. For water, it is 24 hydrogen gas, it is too, so that more ratio of water um over hydrogen guests is to over two. We can therefore say That the number of moles of water that can form in this case will be one times the number of moles of hydrogen gas, And this is equal to one times five moles, Which is just equal to five moles. Right now, let&#x27;s look at the second situation where we start off with 0.15 moles of oxygen. Once again, we need to make use of the mole ratio. So in this case it will be to over one. So the number of moles of water with a number of moles of oxygen will be to over one. This means that the number of moles of water that can form in this case is two times the number of moles off oxygen gas. So this is two times uh 1.50 moles, and this is equal to three moles. Right? So in the first situation, when we started off with five miles off hydrogen gas, We were able to form five moles of water. But in the case of oxygen, we start off with oxygen Um and specifically 1.50 moles of oxygen. Then we can only end up with 3.00 moles of water, which is the least amount produced in the two situations. So because we can only produce a maximum A number of moles of three moles of water, this indicates that oxygen is the limiting reactant here. Oxygen is the limiting reactant. And if we start off with 0.150 miles of oxygen, Then we can produce three moles of water. Right? So to recap in this reaction, we had to identify the limiting reactant first. For this purpose, we compare the number of moles of water that um could form, starting off with the different number of moles off either. Um First of all, we looked at hydrogen gas and then on the other hand, the number of moles of oxygen gas. So in this way we realized that the limiting reactant is oxygen gas because it can only form three moles of water compared to the five mills that can be formed when we start off with the hydrogen gas, is the reactant. Now, if we start off with oxygen 0.15 moles of oxygen gas, then um three miles of water was formed in the end",
              "dateCreated": "Aug. 11, 2021, 12:50 a.m.",
              "upvoteCount": 3,
              "url": "https://www.numerade.com/questions/if-500-mathrmmol-of-hydrogen-gas-and-150-mathrmmol-of-oxygen-gas-react-what-is-the-limiting-reactant/",
              "author": {
                "@type": "Person",
                "name": "Marietjie Lutz"
              }
            },
            
        
            
            {
              "@type": "Answer",
              "text": "for this problem, we&#x27;re gonna be working on understanding limiting reactions and using them to solve for products, were given that we have this chemical equation four, NH three plus 502 yields four N O plus six H 20 Were given that we have 2.35 moles of NH three and 2.75 moles of 02 To work with, we need to understand which of these reactions is the limiting reactant and then use that to figure out how much water we&#x27;re will be produced. It&#x27;s important to figure out which of these is the limiting reactant, because this reaction will only go so far as that reaction allows. So, to figure out which one is the limiting reactant, we can choose to use either the NH three or the 02 It doesn&#x27;t matter. I&#x27;m gonna go with the NH three. So I&#x27;m going to lay out what I have, I have 2.35 moles of NH three. Next thing I&#x27;m gonna do is I&#x27;m going to look at our ratio by looking at the coefficients in our balanced equation and see that for every four moles of NH three, we&#x27;re going to also use five moles of 02 So the way to work this out is I&#x27;m going to multiply 2.35 by five and then whatever I get from that, I will then divide by four. And when I do that I get 2.94 malls of 02 because our moles of NH three will cancel out. So then I&#x27;m going to go look at how much 02 were given. 2.75 Well, that is that is less than 2.94 So, what this means is that we do not have enough 02 to fully react with the NH three that were given. So that means that are limiting reactant is 02 The next thing we&#x27;re gonna do is we&#x27;re going to use that limiting reactant to solve for another ratio like this to find out how much water is going to be produced. So we have 2.75 moles of 02 to work with. We&#x27;re going to set up our ratio again for every five moles of 02 we can create six moles of H 20 I&#x27;m going to multiply 2.75 by six and then divide that answer by five to get 3.30 moles of H 20 and that is how much water we can produce.",
              "dateCreated": "Aug. 11, 2021, 12:50 a.m.",
              "upvoteCount": 3,
              "url": "https://www.numerade.com/questions/in-the-following-reaction-235-mathrmmol-of-mathrmnh_3-reacts-with-275-mathrmmol-of-mathrmo_2-how-man/",
              "author": {
                "@type": "Person",
                "name": "Shaelyn Deal"
              }
            },
            
        
            
            {
              "@type": "Answer",
              "text": "in this question, Methane gas reacts with oxygen in order to form carbon dioxide and water. So this is a combustion reaction and we start off with one mole of methane gas and five moles of oxygen gas. Now we need to determine the limiting reactant here so that we can determine the number of moles of water that perform in the end. In order to determine that limiting reactant, we will compare the number of moles of water that can be formed Firstly, if we start off with one mole of the fungus and secondly, then if we start off with five miles off oxygen gas. So when we compare these two situations, we will be able to identify the limiting reactive. Now, in order to determine the number of malls Can be formed from one mole of methane gas, we make use of the mole ratio. So we know that geometric coefficient of water six and has a documentary coefficient of two. So the mole ratio of water to six, the kids. Therefore the number of moles of water can be formed in this case 6/2&quot;, 3 times the number of Malzahn. Yes. Right. And we know we saw it off with um five starting with one mole of methane gas. And therefore this number of northern waters will be $3.1 which is equal to three months. Right. So let&#x27;s have a look at the second situation where we choose to start off with the oxygen as our reacted. No, once again, you wanted to To calculate the number of moles of water that can be produced. We need to make use of the mole ratio more racial. In this case of water to oxygen is 6-7. So the number of moles of water Over the number of number of moles of oxygen will be 6, 7. So the number of moles of water that can be 46 or seven times oxygen. So it&#x27;s 6/7 times. We started off with five levels of oxygen, six of the seven times 5 And that is equal to round off to two decimal places. 4.29. Uh huh. So now I need to compare the number of moles that can be formed in these two different situations to firstly, when I started off with one more of methane gas, The reaction um we&#x27;re able to produce three moles of water. But if I started off with five months of oxygen gas, We actually were able to produce, 4.29 mi of water. So therefore the maximum number of moles that can be produced in this case. Yes, three. And that is from using um it&#x27;s in gas, which is the limiting reactant. So we started off with a balanced equation and we had to identify the limiting reactant in this case by and we did that by comparing the number of moles of water that could form by starting off in the first place with one more of anything goes. In the second place, five moles of oxygen within. Saw that um The methane gas could not produce more than three miles. Um whereas the oxygen case of the oxygen, Um the reaction produced 4.29 moles. So because we could only produce three moles by using one move methane gas, this is the maximum number of moles that could be produced in this reaction. And therefore we also know that um the limiting reacting to here is a same gas.",
              "dateCreated": "Aug. 11, 2021, 12:50 a.m.",
              "upvoteCount": 3,
              "url": "https://www.numerade.com/questions/if-100-mathrmmol-of-ethane-gas-and-500-mathrmmol-of-oxygen-gas-react-what-is-the-limiting-reactant-a/",
              "author": {
                "@type": "Person",
                "name": "Marietjie Lutz"
              }
            }
            
        
      ]
    }
  }
</script>

CodePudding user response:

Basically, I was trying to read broken JSON, as Felix mentioned JSON cannot contain literal line breaks.

Solution: use https://www.npmjs.com/package/jsonrepair module. It detected the bad lines and fixed them, this is likely what google does (some sort of JSON repair).

PS: I tried https://www.npmjs.com/package/json-fixer without success

  • Related