0

I have a list like this:

<ol class="CricketPlayers">
    <li id="1">Player: John, TotalRuns: 4350, AllRounder: false</li>
    <li id="2">Player: Michael, TotalRuns: 8373, AllRounder: true</li>
</ol>

I need to convert this into JSON like this:

[
    {
        "Player": "John",
        "TotalRuns": 4350,
        "AllRounder": false
    },
    {
        "Player": "Michael",
        "TotalRuns": 8373,
        "AllRounder": true
    }
]

Formatting of JSON is not important.

Using jQuery etc. is not an option. I am thinking to convert the list into array and then calling JSON.stringify. But I not sure if that will work because there can be many items in the list.

3
  • 1
    What's the specific issue? You'll need to iterate over the children of .CricketPlayers, pull the text out of each <li>, split on , (and hope there are no other commas), iterate over those and split on : and use each pair as a property name/value. This is why web scraping is awful--can you just get the data instead? Commented Apr 22, 2021 at 16:04
  • @DaveNewton I can start formatting it as JSON when data is entered, but it can be deleted and then deleting it from that variable is even more difficult. Commented Apr 22, 2021 at 16:24
  • It shouldn't be; you have a unique ID and/or a linear collection, so updating or deleting should be (reasonably) straight-forward. Commented Apr 22, 2021 at 16:28

3 Answers 3

1

try this:

let result = [];
let parent = document.querySelector(".CricketPlayers").children;
Array.prototype.forEach.call(parent, child => {
  let x = child.innerHTML.split(",");
  let y = JSON.stringify(x).replace("[","{").replace("]","}");
  result.push(y)
});
console.log(result)
Sign up to request clarification or add additional context in comments.

Comments

0

Here you go:

[...document.querySelectorAll('.CricketPlayers li')]
.map(node => node.innerText.match(/Player\: (\w+). TotalRuns\: (.*). AllRounder\: (.*)/))
.map(matchedParts => ({ Player: matchedParts ? matchedParts[1] : '', TotalRuns: matchedParts ? matchedParts[2] : 0, AllRounder: matchedParts ? matchedParts[3] : 0 }))

and the results are like yours.

1 Comment

Thanks. Can you please show the output? When I console.log the whole thing, I did not see the result as JSON.
0

Get the children of the ol, iterate them and construct the array.

let ol = document.getElementsByClassName('CricketPlayers');


let output =[...ol[0].children].map(item => {

   let props = item.innerHTML.split(',');
   
   return props.reduce((a, c) => {   
   
      aProp = c.split(':');    
      a[aProp[0].trim()] = aProp[1].trim();    
      return a
    
   }, {})

})

console.log(output)
<ol class="CricketPlayers">
    <li id="1">Player: John, TotalRuns: 4350, AllRounder: false</li>
    <li id="2">Player: Michael, TotalRuns: 8373, AllRounder: true</li>
</ol>

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.