Skip to content

🫐 Rich rendering of JSON as HTML in Go

License

Notifications You must be signed in to change notification settings

nikolaydubina/htmljson

Repository files navigation

🫐 htmljson: rich rendering of JSON as HTML in Go

codecov Go Report Card Go Reference Mentioned in Awesome Go OpenSSF Scorecard

  • pure Go
  • no Javascript
  • no dependencies
  • no reflect
  • no fmt
  • 300 LOC
  • customizable rendering
  • JSON Path for elements access

// JSON has to be any
var v any
json.Unmarshal(exampleJSON, &v)

htmlPage := htmljson.DefaultPageMarshalerm.Marshal(v)
// JSON has to be any
var v any
json.Unmarshal(exampleJSON, &v)

// customize how to render HTML elements
s := htmljson.Marshaler{
    Null:   htmljson.NullHTML,
    Bool:   htmljson.BoolHTML,
    String: htmljson.StringHTML,
    Number: func(k string, v float64, s string) string {
        if k == "$.cakes.strawberry-cake.size" {
            return `<div class="json-value json-number" style="color:red;">` + s + `</div>`
        }
        if v > 10 {
            return `<div class="json-value json-number" style="color:blue;">` + s + `</div>`
        }
        return `<div class="json-value json-number">` + s + `</div>`
    },
    Array: htmljson.DefaultArrayHTML,
    Map:   htmljson.DefaultMapHTML,
    Row:   htmljson.DefaultRowHTML{Padding: 4}.Marshal,
}

m := htmljson.DefaultPageMarshaler
m.Marshaler = &s

// write HTML page
htmlPage := m.Marshal(v)

Related Work