jeremywohl / flatten Goto Github PK
View Code? Open in Web Editor NEWFlatten nested maps or JSON structures to one-dimensional scalar sets.
License: MIT License
Flatten nested maps or JSON structures to one-dimensional scalar sets.
License: MIT License
Why are lists flattened for FlattenString
but not for Flatten
?
Example (Try on go playground):
package main
import (
"fmt"
"github.com/jeremywohl/flatten"
)
func main() {
nested_json := `{
"one": {
"two": [
"2a",
"2b"
],
"three": []
},
"side": "value"
}`
flat_json, _ := flatten.FlattenString(nested_json, "", flatten.DotStyle)
fmt.Println(flat_json)
nested_map := map[string]interface{}{
"one": map[string]interface{}{
"two": []string{"a", "b"},
"three": []string{},
},
"side": "value",
}
flat_map, _ := flatten.Flatten(nested_map, "", flatten.DotStyle)
fmt.Println(flat_map)
}
Results:
{"one.two.0":"2a","one.two.1":"2b","side":"value"}
map[one.three:[] one.two:[a b] side:value]
Hi all,
Is there any option to keep the order of Flattned Map same with origion JSON string ?
See following example:
flat, err := flatten.FlattenString("null", "", flatten.DotStyle)
fmt.Printf("error: %v\n", err)
fmt.Printf("v: %s\n", flat)
flat, err = flatten.FlattenString("a", "", flatten.DotStyle)
fmt.Printf("error: %v\n", err)
fmt.Printf("v: %s\n", flat)
https://play.golang.org/p/AKQWQcY_VA6
String literal value is provided in both examples ("null"
and "a"
). We got no error for the first case with {}
value as an output where for the second example the output spawns the error.
I would expect an error.
I know this is an edge case and both behaviors are acceptable. I propose to at least add a test for it. I am happy to contribute once it's clear what to do.
input:
{
"a": [],
"b": 1
}
want:
{"a": [], "b": 1}
but got:
{"b": 1}
It might be nice to support flattened paths that distinguish between list indexes and object keys.
E.g., this:
{
"foo": {
"0": ["bar", {"baz": "bam"}],
}
}
might be flattened to:
{
"foo.0[0]": "bar",
"foo.0[1].baz": "bam",
}
Hi,
Nice library! Have you considered adding support for reversing, i.e. unflattening? If I slightly change the example in the README:
input := `{ "one.two.0": "2a", "one.two.1": "2b", "side": "value" }`
nested, err := flatten.UnflattenString(input, "", flatten.DotStyle)
/*
nested == {
"one": {
"two": [
"2a",
"2b"
]
},
"side": "value"
}
*/
input:
{
"a": {
"b": [1, 2, 3]
}
}
output:
{"a.b": [1, 2, 3]}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.