question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I get json string from cheerio object?

See original GitHub issue

not found anywhere.

How can I get json string from cheerio object? NOT OBJECT, but STRING

for example:

<xml>
  <some1></some1>
  <some2></some2>
</xml>

json (string):

xml {
 some1, 
 some2
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
dbuezascommented, Dec 3, 2017

Just walk the tree yourself and grab what you need on the way, the DOM is represented as a javascript object already in cheerio.

walk = ({ tagName, attribs, children = [] }) => ({
  tagName,
  attribs,
  children: children.map(walk),
}); 
JSON.stringify(walk($('html')[0]), null , 2);

if you want the doctype then start the walk on $._root. Technically you should use cheerio’s getters instead of accessing the properties directly but whatever.

0reactions
fb55commented, Jan 28, 2018

Assuming this is solved!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert cheerio object to string - Stack Overflow
let arrayOfHTMLstrings = $('meta').toArray().map( (el, index) => el.toString() );. According to cheerio official doc.
Read more >
Cheerio Script for Turning HTML Pages Into JSON Files
For this demo, I will use Cheerio to build a simple web scraper that gets all the content from a page and outputs...
Read more >
cheerio
First you need to load in the HTML. This step in jQuery is implicit, since jQuery operates on the one, baked-in DOM. With...
Read more >
Using the Cheerio NPM Package for Web Scraping
In this article, you'll learn how to use Cheerio to scrape data from static ... page and parses the content of a table...
Read more >
How to Scrape Web Pages with Cheerio in Node.js - ZenRows
This tutorial will take you through how to use Cheerio to scrape Pokémon data from an example website, and then create a JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found