json-flattener helps in flattening complex nested JSONs in different ways
go get github.com/anshal21/json-flattener
jsonStr := `{
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
]
}`
flattnedJSON, err := flattener.FlattenJSON(jsonStr, flattener.DotSeparator)
{
"author.firstname": "Jane",
"author.lastname": "Doe",
"category.0": "Non-Fiction",
"category.1": "Technology",
"editor.firstname": "Jane",
"editor.lastname": "Smith",
"isbn": "123-456-222",
"title": "The Ultimate Database Study Guide"
}
jsonStr := `{
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
]
}`
flattnedJSON, err := flattener.FlattenJSON(jsonStr, flattener.DotSeparator, flattener.IgnoreArray())
{
"author.firstname": "Jane",
"author.lastname": "Doe",
"category": [
"Non-Fiction",
"Technology"
],
"editor.firstname": "Jane",
"editor.lastname": "Smith",
"isbn": "123-456-222",
"title": "The Ultimate Database Study Guide"
}
jsonStr := `{
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"v1.0.0": {
"lastname": "Smith",
"firstname": "Jane"
},
"v2.0.0": {
"lastname": "Doe",
"firstname": "John"
}
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
]
}`
flattnedJSON, err := flattener.FlattenJSON(jsonStr, flattener.DotSeparator, flattener.WithDepth(2))
{
"author.firstname": "Jane",
"author.lastname": "Doe",
"category.0": "Non-Fiction",
"category.1": "Technology",
"editor.v1.0.0": {
"firstname": "Jane",
"lastname": "Smith"
},
"editor.v2.0.0": {
"firstname": "John",
"lastname": "Doe"
},
"isbn": "123-456-222",
"title": "The Ultimate Database Study Guide"
}