jsoner icon indicating copy to clipboard operation
jsoner copied to clipboard

Serialize HTML tables into JSON in Ruby

Jsoner

Serialize HTML tables into JSON in Ruby.
Gem Version Build Status

Installation

Add this line to your application's Gemfile:

gem 'jsoner'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsoner

Usage

html = <<-eohtml
    <table id='example-table'>
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Points</th></tr>
      </thead>
      <tbody>
        <tr>
          <td>Jill</td>
          <td>Smith</td>
          <td>50</td></tr>
        <tr>
          <td>Eve</td>
          <td>Jackson</td>
          <td>94</td></tr>
        <tr>
          <td>John</td>
          <td>Doe</td>
          <td>80</td></tr>
        <tr>
          <td>Adam</td>
          <td>Johnson</td>
          <td>67</td></tr>
      </tbody>
    </table>
  eohtml

# Convert HTML table into Json

json = Jsoner.parse(html)

# output json => 
# 
#   [ {"First Name"=>"Jill", "Last Name"=>"Smith",   "Points"=>"50"},
#     {"First Name"=>"Eve",  "Last Name"=>"Jackson", "Points"=>"94"},
#     {"First Name"=>"John", "Last Name"=>"Doe",     "Points"=>"80"},
#     {"First Name"=>"Adam", "Last Name"=>"Johnson", "Points"=>"67"} ]

Or, Jsoner can parse HTML file including HTML table

require 'open-uri'
# you must have table.html file and assign file path correctly.

table = Jsoner.parse(open('table.html'))

# output json => 
# 
#   [ {"First Name"=>"Jill", "Last Name"=>"Smith",   "Points"=>"50"},
#     {"First Name"=>"Eve",  "Last Name"=>"Jackson", "Points"=>"94"},
#     {"First Name"=>"John", "Last Name"=>"Doe",     "Points"=>"80"},
#     {"First Name"=>"Adam", "Last Name"=>"Johnson", "Points"=>"67"} ]

Or you can open Link

require 'open-uri'

table = Jsoner.parse(open('http://www.w3school.com.cn/tiy/t.asp?f=html_table_header'))

# output json => 
#
# [{"姓名"=>"Bill Gates", "电话"=>"555 77 855"}]

THANKS

table-to-json written by @lightswitch05 in JavaScript.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request