Bob Cen

Greenhorn
+ Follow
since Jul 08, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bob Cen

Jesper Young wrote:

I did not say there is absolutely no way to do this. But there is no clear way to do this in general. Your example with the Firefox bookmarks is a very specific example, with a specific mapping. It's not a general mapping that converts any HTML page to JSON.

If the page is in XHTML (a variant of HTML that conforms to XML syntax - i.e. all elements have open and close tags, all attributes have quoted values etc.) then you don't even need to do the HTML to XML conversion first. But you'd get a JSON document that has the same structure as the HTML, for example:

<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Some text</p>
</body>
</html>

converts to:

"html": { "head": { "title": "Hello World" }, "body": { "p": "Some text" } }

You could do that if you want to. I'm curious however why you'd want to do this, to me it looks like a strange way to use JSON.



Thank you for your response. My initial reason has to do with Firefox and the changes they made in Release 3 and later. I have a number of different bookmark files that I (eventually) need to get in sync. Although, Firefox provides an import function, it doesn't work the same way as it did in prior releases. It basically "appends" the import to an existing bookmark file and you need to "organize bookmarks" and "edit" to get what you want. Just copying your (legitimate) "bookmark" file into the Firefox directory does not work as Firefox overlays it with the prior bookmark file. Just deleting the backups doesn't solve the problem either (although I haven't tried that in conjunction with deleting the "places.sqlite" file). One way to get what you want would be to convert the HTML file to a JSON file and save it with the proper filename in the Firefox backup directory. Then you can "restore" to an earlier bookmark file (JSON) to get the bookmarks you really want. It is unfortunate that Firefox did not allow for a "blanket replace" (or import) of a bookmark file with the latest releases. Anyway, this is one reason why an HTML to JSON converter wuld be useful. There are also other situations. My google searches have turned up a number of people searching for HTML <-> JSON converters and some searches have come up with "convoluted" ways of doing it.
13 years ago

Jesper Young wrote:

Bob Cen wrote:Now, do you know of an actual HTML to JSON converter? I am looking for a straight-forward Yes or No answer, not ...


In that case... no.

If you're not interested why not, then stop reading now. Otherwise...

HTML is a totally different kind of format than JSON. JSON (JavaScript Object Notation) is a way to serialize objects (you have an object, which contains key-value pairs etc.). HTML describes a page layout. That doesn't fit very well with what JSON represents. There is no clear, one-to-one way to map the structure of an HTML document to objects with key-value pairs. They are just two totally different formats, made for different purposes.

What's the reason why you need this? If you explain what your actual goal is, we might be able to help you further.



The format of HTML and JSON (and the purpose of HTML and JSON) is irrelevant. Converters are written all the time, given the definition of what has to be converted. Granted, some conversions are not 100%, but even 99% is better than none. As far as my question and your response, which implies there is no way to convert between the two formats, is not exactly accurate. Firefox converts HTML to JSON and JSON to HTML. It does this for bookmarks. When you start with a fresh install of Firefox, you get a bookmarks.html "starter file". As you add bookmarks to this file, Firefox will save the updated bookmarks.html as a JSON file in the bookmarksbackup folder. Later, if the need arises, you can restore a bookmarks.html file from a "saved" JSON backup. Granted, there may be certain rules to be followed in the process used by Firefox, which other HTML <-> JSON applications may not follow, but Firefox shows it can be done in both directions. I may be wrong, but I suspect if what I have seen I have interpreted correctly (and maybe not), a 2-way converter could be written. My understanding is JSON was written as an alternative to XML. Now there are XML <-> HTML converters (XML <-> other formats -- OmniFormat, Crystal Passage, etc.).

Actually, this discussion has made me think about it (as I wasn't thinking of XML in my initial post). There are XML <-> JSON converters. So I could do the conversion in 2 steps HTML -> XML and then XML -> JSON (and, of couse, reverse the procedure to go the other way). Thank you. javascript:emoticon('');
13 years ago

Jesper Young wrote:Which question are you asking:

- "is there a way to convert HTML to JSON"
- "is there a way to convert JSON to HTML"

Because in the title you're asking one thing, but in the topic itself you're asking the opposite.



Actually, I meant HTML to JSON conversion. I can see a need for both converters - HTML to JSON and JSON to HTML! Now, do you know of an actual HTML to JSON converter? I am looking for a straight-forward Yes or No answer, not do 3 backflips, a 3 hour headstand, followed by three moer backflips to get there (as I have seen posted by a number of answers to this same question). Also, I am not looking for another question as to why I want to do it that way as that is irrelevant to the question - stating my reasons just brings answers as I just described - "do 3 backflips ...". Thank you.

BTW, the title on this "page" states "is there a way to convert html to JSON?", which I believe is what was my question. Thanks.
13 years ago

Ulf Dittmer wrote:There is no general conversion from HTML to JSON, nor do I think it would make much sense to define one (which isn't to say it wouldn't make sense for your purposes). If I wanted to work with HTML I'd convert it to XML (using a library like NekoHTML or TagSoup) and then use XML/DOM APIs to manipulate it.



I think you just defined a need to have a HTML to JSON converter. Having to go through 2 additional steps to achieve what could be done in a single step is not the most desirable way of doing something. Having said that, has anyone developed a HTML to JSON converter yet? I could use one as I have need to convert a number of HTML files (without going into my reasons for doing it this way). Thank you.
13 years ago