Documentation
¶
Overview ¶
Package jsonconv provides functionality for flattening JSON objects and converting JSON to CSV. It supports deep nested JSON structures and offers flexible configuration options.
Index ¶
Constants ¶
const ( // FlattenLevelUnlimited can be set to FlattenOption.Level for unlimited flattening. FlattenLevelUnlimited = -1 // FlattenLevelNonNested can be set to FlattenOption.Level for non-nested flattening // (equivalent to non-flattening). FlattenLevelNonNested = 0 )
const ( // DefaultFlattenLevel can be set to FlattenOption.Level for default level flattening // (equivalent to FlattenLevelUnlimited). DefaultFlattenLevel = FlattenLevelUnlimited // DefaultFlattenGap can be set to FlattenOption.Gap for default gap flattening. DefaultFlattenGap = "__" )
const CsvComma rune = ','
CsvComma is the default field delimiter for CSV files.
Variables ¶
var DefaultFlattenOption = &FlattenOption{ Level: DefaultFlattenLevel, Gap: DefaultFlattenGap, }
DefaultFlattenOption provides default settings for flattening operations.
Functions ¶
func CreateCsvHeader ¶
CreateCsvHeader creates []string from arr and baseHs. A baseHs is base header that we want to put at the beginning of dynamic header, we can set baseHs to nil if we just want to have dynamic header only.
func Flatten ¶
func Flatten(obj map[string]any, opt *FlattenOption)
Flatten flattens obj with given opt. If opt is nil, it will use opt value from DefaultFlattenOption instead.
Types ¶
type CsvWriter ¶
type CsvWriter struct {
// Field delimiter. Set to ',' (CsvComma) by default in NewCsvWriter
Delimiter rune
// True to use \r\n as the line terminator
UseCRLF bool
// contains filtered or unexported fields
}
A CsvWriter writes records using CSV encoding.
func NewCsvWriter ¶
NewCsvWriter returns a new CsvWriter that writes to w.
type FlattenOption ¶
type FlattenOption struct {
// Level of flattening, it can be FlattenLevelUnlimited,
// FlattenLevelNonNested or an int value in [1..n]
Level int
// A gap between nested JSON and its parent JSON.
// It will be used when merging nested JSON's key with parent JSON's key
Gap string
// Skip Map type (typically JSON Object type) from flattening process
SkipMap bool
// Skip Array type (JSON array, string array, int array, float array, etc.)
// from flattening process
SkipArray bool
}
A FlattenOption is for JSON object flattening.
type JsonReader ¶
type JsonReader struct {
// contains filtered or unexported fields
}
A JsonReader reads and decodes JSON values from an input stream.
func NewJsonReader ¶
func NewJsonReader(r io.ReadSeeker) *JsonReader
NewJsonReader returns a new JsonReader that reads from r.
func (*JsonReader) Read ¶
func (r *JsonReader) Read(v any) error
Read reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
type JsonWriter ¶
type JsonWriter struct {
// EscapeHTML specifies whether problematic HTML characters
// should be escaped inside JSON quoted strings.
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
// to avoid certain safety problems that can arise when embedding JSON in HTML.
EscapeHTML bool
// contains filtered or unexported fields
}
A JsonWriter writes JSON values to an output stream.
func NewJsonWriter ¶
func NewJsonWriter(w io.Writer) *JsonWriter
NewJsonWriter returns a new JsonWriter that writes to w.
func (*JsonWriter) Write ¶
func (r *JsonWriter) Write(v any) error
Write writes the JSON encoding of v to the stream, followed by a newline character.
type ToCsvOption ¶
type ToCsvOption struct {
// Set it to apply JSON flattening
FlattenOption *FlattenOption
// Base CSV headers used to add before dynamic headers
BaseHeaders []string
}
A ToCsvOption converts a JSON Array to CSV data.