flattener

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2021 License: MIT Imports: 2 Imported by: 0

README

Go Report Card

json-flattener

json-flattener helps in flattening complex nested JSONs in different ways

Installation

go get github.com/anshal21/json-flattener

Examples

1. Simple Flattening
   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"
    }
2. Flatten and ignore arrays
   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"
    }
3. Flatten till specified depth
   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"
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlattenJSON

func FlattenJSON(JSONStr string, separator Separator, options ...Option) (string, error)

FlattenJSON flattens the provided JSON The flattening can be customised by providing flattening Options

Types

type Option

type Option func(f *flattenerConfig)

Option ...

func IgnoreArray

func IgnoreArray() Option

IgnoreArray option, if enabled, ignores arrays while flattening

func WithDepth

func WithDepth(depth int) Option

WithDepth option, if provided, limits the flattening to the specified depth

type Separator

type Separator string

Separator ...

const (
	DotSeparator Separator = "."
)

Separators ...

func (Separator) String

func (s Separator) String() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL