flattenjson

package module
v0.0.0-...-6deabd0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2016 License: GPL-3.0 Imports: 3 Imported by: 1

README

flattenjson

What is flattenjson?

flattenjson is a go library to flatten nested JSON structure.

How does it work?

package main

import (
    "fmt"

    "github.com/monodeep12/flattenjson"
)
func main() {
    input := []byte(`{
  "_id": "57145f82f7579b1b172afdef",
  "brand_info": {
      "media_plan_name": "Media Plan 3",
      "brand_name": "Dabur",
      "product_category": "Shoes",
      "product_sub_category": "Formal Shoes",
      "gender": ["Male"],
      "age": ["35+","15-35"],
      "audience_type": ["Urban"],
      "geography": ["BLR"]
  }
}`)
  // passing `flattenSlice` flag as `true` will result in flattening the inner slices as well 
	out1, err := flattenjson.JSONByte(input, ".", true)
	// passing `flattenSlice` flag as `false` will result in not flattening the inner slices
	out2, err := flattenjson.JSONByte(input, ".", false)
	if err != nil {
		// handle error
	}
	fmt.Println(string(out1))
	fmt.Println(string(out2))
}

Output1:

{"_id":"57145f82f7579b1b172afdef","brand_info.age.0":"35+","brand_info.age.1":"15-35","brand_info.audience_type.0":"Urban","brand_info.brand_name":"Dabur","brand_info.gender.0":"Male","brand_info.geography.0":"BLR","brand_info.media_plan_name":"Media Plan 3","brand_info.product_category":"Shoes","brand_info.product_sub_category":"Formal Shoes"}

Output2:

{"_id":"57145f82f7579b1b172afdef","brand_info.age":["35+","15-35"],"brand_info.audience_type":["Urban"],"brand_info.brand_name":"Dabur","brand_info.gender":["Male"],"brand_info.geography":["BLR"],"brand_info.media_plan_name":"Media Plan 3","brand_info.product_category":"Shoes","brand_info.product_sub_category":"Formal Shoes"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONByte

func JSONByte(jsonByte []byte, delimeter string, flattenSlice bool) ([]byte, error)

JSONByte takes in a nested JSON as a byte array and a delimiter and returns an flattened json byte array jsonByte = JSON byte array delimeter = character used to separate keys flattenSlice = bool to decide weather to flatten slices within the JSON

Types

This section is empty.

Jump to

Keyboard shortcuts

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