Categorygithub.com/anshal21/json-flattener
repositorypackage
1.0.0
Repository: https://github.com/anshal21/json-flattener.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

License: MIT 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"
    }