My New Hugo Site

  1. Bash
    1. Filters
      1. grep
      2. Sed
      3. jq
    2. Shellspec
  2. Design
  3. Architectural Patterns
    1. Systemd
    2. Message Broker
    3. JSON-RPC
  4. Go
    1. Concurrency
    2. Web Applications
    3. Compound Data
    4. Json
    5. Go vs Erlang
  5. Prolog Cookbook
  6. Documentation
    1. Hugo
      1. Go Html Template
      2. Table of Contents
    2. HTML
    3. CSS
      1. Color
      2. Style Guides
      3. Layout
    4. Mathjax
  7. Visualization
    1. D3
      1. Venn Diagrams
    2. SVG
    3. Visjs
      1. Network
  8. Data
    1. Yaml
    2. Events
      1. JSON-LD
    3. JSON
      1. jCal
    4. SQL
  9. JavaScript

Json

https://mholt.github.io/json-to-go/

https://www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body

https://stackoverflow.com/questions/17156371/how-to-get-json-response-from-http-get

RFC 7159

JSON GO
false, null, true
Objects {key1:value1, key2:value2, …} map[string]interface{}
Arrays [value1, value2, ... []interface{}
Numbers
Strings

encoding/json

json.NewDecoder

Go does not appear to allow alternative types, and func Unmarshal(data []byte, v interface{}) error puts everything in a catchall type empty interface interface{}.

These require type assertion to make them usable.

https://www.digitalocean.com/community/tutorials/how-to-use-interfaces-in-go

https://stackoverflow.com/questions/14025833/range-over-interface-which-stores-a-slice

reflect

JSON and Go

package main

import (
  "fmt"
  "log"
  "os"
  "encoding/json"
  "reflect"
)

func main() {
  
  jsonBlob, err := os.ReadFile("/home/roblaing/webapps/hugo/content/joeblog/events.json")
  /* jsonBlob, err := os.ReadFile("array.json") */
  if err != nil {
    log.Fatal(err)
  } else {
    var jobj map[string]interface{}
    jerr := json.Unmarshal(jsonBlob, &jobj)
	if jerr != nil {
      fmt.Println("error:", jerr)
	}
    /* only works for jobj type map[string]interface{}, could be []interface{} */
    for k := range jobj {
      switch jobj[k].(type) {
        case string: 
          fmt.Printf("%v %v\n", k, jobj[k])
        case []interface{}:
          s := reflect.ValueOf(jobj[k])
          for i := 0; i < s.Len(); i++ {
            fmt.Printf("%T\n", s.Index(i).Interface())
          }
        default: 
        fmt.Printf("%v %T\n", k, jobj[k])
      }
    }
  }

}

The empty interfaceinterface{} is used to translate the {key: val, …} in the json file to a variable (keyvals above) holding a map[…].