https://github.com/bruth/flatjson
Flatten nested JSON documents.
https://github.com/bruth/flatjson
Last synced: 9 months ago
JSON representation
Flatten nested JSON documents.
- Host: GitHub
- URL: https://github.com/bruth/flatjson
- Owner: bruth
- Created: 2015-08-28T15:04:22.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-28T15:08:56.000Z (almost 11 years ago)
- Last Synced: 2025-02-14T10:56:04.761Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 125 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# flatjson
[](https://godoc.org/github.com/bruth/flatjson)
The `flatjson` package provides an encoder to take a value or existing JSON string and convert it into a flat map or array or key-value pairs. For example, the following document can be flattened to an array of key-value pairs:
```json
{
"name": "Bob Smith",
"address": {
"street": "123 Main Street",
"city": "Boresville",
"zipcode": 13943
},
"hobbies": ["tennis", "coding", "cooking"]
}
```
```json
[
["name", "Bob Smith"],
["address.street", "123 Main Street"],
["address.city", "Boresville"],
["address.zipcode", 13943],
["hobbies[0]", "tennis"],
["hobbies[1]", "coding"],
["hobbies[2]", "cooking"]
]
```
## What is the motivation?
Working with a flat set of a key value pairs is often easier to work with when storing or indexing nested documents. It also makes it easier to compare documents by just iterating over the keys and comparing values rather than recursing into documents or arrays.
## Install
Get the library.
```
go get github.com/bruth/flatjson
```
Install the command line tool.
```
go install github.com/bruth/flatjson/cmd/flatjson
```
## Library Example
Convert existing JSON into flat format.
```go
import (
"strings"
"github.com/bruth/flatjson"
)
var json = `
{
"name": "Bob Smith",
"address": {
"street": "123 Main Street",
"city": "Boresville",
"zipcode": 13943
},
"hobbies": ["tennis", "coding", "cooking"]
}
`
r := strings.NewReader(json)
// Write the JSON to standard out.
if err := flatjson.NewEncoder(os.Stdout).ConvertMap(r); err != nil {
// Handle error..
}
```
## CLI Tool
```
flatjson [-array] [file]
```
### Example
```
flatjson
{
"name": "Bob Smith",
"address": {
"street": "123 Main Street",
"city": "Boresville",
"zipcode": 13943
},
"hobbies": ["tennis", "coding", "cooking"]
}
{
"address.street": "123 Main Street",
"address.city": "Boresville",
"address.zipcode": 13943,
"hobbies[0]": "tennis",
"hobbies[1]": "coding",
"hobbies[2]": "cooking",
"name": "Bob Smith"
}