top

Package jsonq simplify your json usage with a simple hierarchical query.

Given some json data like:

{
	"foo": 1,
	"bar": 2,
	"test": "Hello, world!",
	"baz": 123.1,
	"array": [
		{"foo": 1},
		{"bar": 2},
		{"baz": 3}
	],
	"subobj": {
		"foo": 1,
		"subarray": [1,2,3],
		"subsubobj": {
			"bar": 2,
			"baz": 3,
			"array": ["hello", "world"]
		}
	},
	"bool": true
}

Decode it into a map[string]interrface{}:

import (
	"strings"
	"encoding/json"
	"github.com/jmoiron/jsonq"
)

data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(jsonstring))
dec.Decode(&data)
jq := jsonq.NewQuery(data)

From here, you can query along different keys and indexes:

// data["foo"] -> 1
jq.Int("foo")

// data["subobj"]["subarray"][1] -> 2
jq.Int("subobj", "subarray", "1")

// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")

// data["subobj"] -> map[string]interface{}{"subobj": ...}
obj, err := jq.Object("subobj")

Notes:

Missing keys, out of bounds indexes, and type failures will return errors. For simplicity, integer keys (ie, {"0": "zero"}) are inaccessible by `jsonq` as integer strings are assumed to be array indexes.

Imported by 82 package(s)

  1. bosun.org/cmd/bosun/sched
  2. github.com/CaliDog/certstream-go
  3. github.com/DudeofA/jewbot
  4. github.com/DudeofA/jewvis
  5. github.com/DudeofA/mumbledj
  6. github.com/JonathanUsername/guardian-hivemind-markov
  7. github.com/JonathanUsername/slack_markov
  8. github.com/Shadowmad/booksmart-api-server
  9. github.com/THUNDERGROOVE/census
  10. github.com/ZloyDyadka/monet/gallery
  11. github.com/aditya-dhende/cmpe273-assignment3
  12. github.com/akoy93/hobo_signs_server
  13. github.com/ammario/mcping
  14. github.com/angdev/chocolat/api
  15. github.com/bpicolo/picfit/application
  16. github.com/buger/cloud-ssh
  17. github.com/chinakr/monet/gallery
  18. github.com/cleesmith/golang_learning/jsonq
  19. github.com/cleesmith/golang_learning/twitter
  20. github.com/cmacrae/xur-notify
  21. github.com/codahale/librato-collect
  22. github.com/damienstanton/gimages
  23. github.com/davemt/dumbdumb/handler
  24. github.com/figoxu/goPraticse/json/jsonq
  25. github.com/fort-pinnsvin/travel/handlers
  26. github.com/gabstv/go-mgoplus
  27. github.com/gardener/gardenctl/cmd
  28. github.com/greyia/mimorin_downloader
  29. github.com/hemantasapkota/insta/commander
  30. github.com/henteko/dgate-invite/dgate
  31. github.com/heptiolabs/certstream-slack
  32. github.com/infinitbyte/framework/core/api
  33. github.com/infinitbyte/gopa/api
  34. github.com/inhies/monet/gallery
  35. github.com/iron-io/functions/fn/commands
  36. github.com/ivangonekrazy/swirlpool
  37. github.com/jfbus/picfit/application
  38. github.com/jimmyplaza/MonitorServer
  39. github.com/jmoiron/monet/gallery
  40. github.com/jonog/redalert/assertions
  41. github.com/kamlendrachauhan/cmpe273-assignment2
  42. github.com/kamlendrachauhan/cmpe273-assignment3
  43. github.com/klrkdekira/j2gg
  44. github.com/luck02/dibbler/service
  45. github.com/medcl/gopa/modules/api/handlers
  46. github.com/medcl/gopa/modules/api/http
  47. github.com/mies/cli
  48. github.com/mixradio/klink/common
  49. github.com/mixradio/klink/exploud
  50. github.com/mixradio/klink/jenkins
  51. github.com/mixradio/klink/onix
  52. github.com/mixradio/klink/ssh
  53. github.com/nsf/picfit/application
  54. github.com/ooclab/etp
  55. github.com/patrickToca/monet/gallery
  56. github.com/patrobinson/go-fish/input
  57. github.com/pedronasser/functions/fn
  58. github.com/poblahblahblah/resourced/queryparser
  59. github.com/rdallman/cli
  60. github.com/recordedfuture/heart_api
  61. github.com/resourced/resourced/queryparser
  62. github.com/resourced/resourced/writers
  63. github.com/reusee/FileStore/baidu
  64. github.com/rightscale/right_api_cmd
  65. github.com/saifabid/chocolat/api
  66. github.com/santanusetu/cmpe273-assignment3
  67. github.com/scusi/certstream-go
  68. github.com/sorintlab/stolon/cmd/sentinel
  69. github.com/telemetryapp/agent_crittercism/crittercism
  70. github.com/terryh/MonitorServer
  71. github.com/tirinox/go-waiter
  72. github.com/tirinox/gowaiter
  73. github.com/tonto/cli
  74. github.com/treeder/functions/fn
  75. github.com/ukd1/baremetrics-sms
  76. github.com/ulule/ipfix/application
  77. github.com/upamune/mimorin_downloader
  78. github.com/wangtuanjie/picfit/application
  79. github.com/wantedly/developers-account-mapper/services
  80. github.com/wlcx/mumbledj
  81. github.com/zond/diplicity/diptest
  82. github.com/zyfdegh/cadvisor/metrics

Imported only in test by 2 package(s)

  1. github.com/rf-/gologs/services
  2. github.com/rtoma/logspout-redis-logstash