Documentation
¶
Overview ¶
Package goflat provides functions for flattening and unflattening JSON objects. It supports flattening JSON arrays as well as nested maps.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FlattenJSON ¶
FlattenJSON flattens a JSON object into a map[string]interface{} using the specified options. It supports flattening JSON arrays as well.
Example:
data := []byte(`{"name": "John", "age": 30, "address": {"city": "New York", "state": "NY"}}`) options := DefaultOptions() flattened, err := FlattenJSON(data, options) if err != nil { fmt.Println("Error:", err) return } fmt.Println(flattened)
Output:
map[address.city:New York address.state:NY age:30 name:John]
func FlattenMap ¶
FlattenMap flattens a map[string]interface{} into a map[string]interface{} using the specified options. It supports flattening nested maps as well.
Example:
data := map[string]interface{}{ "name": "John", "age": 30, "address": map[string]interface{}{ "city": "New York", "state": "NY", }, } options := DefaultOptions() flattened := FlattenMap(data, options) fmt.Println(flattened)
Output:
map[address.city:New York address.state:NY age:30 name:John]
func UnflattenJSON ¶
UnflattenJSON unflattens a flattened JSON object into its original structure.
Example:
flattened := map[string]interface{}{ "address.city": "New York", "address.state": "NY", "age": 30, "name": "John", } options := DefaultOptions() unflattened, err := UnflattenJSON(flattened, options) if err != nil { fmt.Println("Error:", err) return } fmt.Println(unflattened)
Output:
map[address:map[city:New York state:NY] age:30 name:John]
Types ¶
type Options ¶
type Options struct { KeyDelimiter string // The delimiter to use for separating keys in the flattened structure MaxDepth int // The maximum depth for flattening }
Options represents the options for flattening and unflattening JSON.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default options for flattening and unflattening JSON.