https://github.com/zzwx/jsonwalk
jsonwalk.Walk walks arbitrary JSON nodes, unmarshalled with the standard library json.Unmarshall call
https://github.com/zzwx/jsonwalk
Last synced: 3 months ago
JSON representation
jsonwalk.Walk walks arbitrary JSON nodes, unmarshalled with the standard library json.Unmarshall call
- Host: GitHub
- URL: https://github.com/zzwx/jsonwalk
- Owner: zzwx
- License: mit
- Created: 2022-10-01T18:28:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-28T13:19:49.000Z (about 3 years ago)
- Last Synced: 2025-05-21T11:51:39.004Z (8 months ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/zzwx/jsonwalk
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/zzwx/jsonwalk)
`jsonwalk.Walk` walks arbitrary JSON nodes, unmarshalled with the standard library `json.Unmarshall` call, which root node can be any single value supported by JSON: null, bool, string, number, array or object. The aim of this library is a quick analysis of the potentially morphing JSON structure and extracting data from the nodes.
> For a library that implements JSON searching & JSON modification, consider [gjson](https://github.com/tidwall/gjson) and [sjson](https://github.com/tidwall/sjson).
Internally the JSON types are mapped as following:
| JSON | `jsonwalk.NodeValueType` | `go` type |
| --------- | ------------------------ | ------------------------ |
| `null` | `Nil` | `nil` |
| `boolean` | `Bool` | `bool` |
| `string` | `String` | `string` |
| `number` | `Float64` | `float64` |
| `array` | `Array` | `[]interface{}` |
| `object` | `Map` | `map[string]interface{}` |
For every discovered node it calls provided callback, which is accepted in a form of the `WalkCallback` interface.
The callback receives the discovered key, value and node type as `jsonwalk.NodeValueType` for any logic to be preformed based on the already known type assertion.
Map keys, as always, will be discovered in an unpredictable order so if any action depends on the order of such values, it should be made in a separate `Walk`.
Quick example of printing a JSON structure with values:
```go
var f interface{}
err := json.Unmarshal([]byte(src), &f)
if err != nil {
return // deal with error
}
jsonwalk.Walk(&f, jsonwalk.Print{})
```
This built-in `Print{}` struct returns an implementation of the `WalkCallback`. To quickly provide a custom callback there's a `Callback` wrapper that accepts the callback function.
Look into `examples` folder for inspiration.