Skip to content

Commit

Permalink
split by tab (better for cut)
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr committed Oct 2, 2021
1 parent 6cc71d4 commit 6e04388
Show file tree
Hide file tree
Showing 6 changed files with 9,643 additions and 9,643 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Flatten your JSON.

`fj` flattens JSON into a value and its full path per line, so data can be worked on using UNIX tools such as grep/awk/etc.
`fj` flattens JSON into a value and its full path per line, so data can be worked on using UNIX tools such as grep/awk/cut/etc.

<pre>
❯ curl -s "https://api.github.com/repos/golang/go/commits?per_page=1" | <b>fj</b> | grep commit.author
json[0].commit.author.name = "Josh Bleecher Snyder"
json[0].commit.author.email = "josharian@gmail.com"
json[0].commit.author.date = "2021-06-30T16:44:30Z"
json[0].commit.author.name "Josh Bleecher Snyder"
json[0].commit.author.email "josharian@gmail.com"
json[0].commit.author.date "2021-06-30T16:44:30Z"
</pre>

fj can un-flatten too, which is useful to get a subset of JSON:
Expand Down
2 changes: 1 addition & 1 deletion flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
bNull = []byte("null\n")
bDot = []byte{'.'}
bPrefix = []byte("json")
bSep = []byte(" = ")
bSep = []byte{'\t'}
)

func flatten(in io.Reader, out io.Writer, stream bool) {
Expand Down
48 changes: 24 additions & 24 deletions flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ func TestFlatten(t *testing.T) {
in string
out []string
}{
{`null`, []string{`json = null`}},
{`true`, []string{`json = true`}},
{`false`, []string{`json = false`}},
{`42`, []string{`json = 42`}},
{`55.99999`, []string{`json = 55.99999`}},
{`-0.55`, []string{`json = -0.55`}},
{`"hi"`, []string{`json = "hi"`}},
{`null`, []string{`json null`}},
{`true`, []string{`json true`}},
{`false`, []string{`json false`}},
{`42`, []string{`json 42`}},
{`55.99999`, []string{`json 55.99999`}},
{`-0.55`, []string{`json -0.55`}},
{`"hi"`, []string{`json "hi"`}},
{`{}`, []string{}},
{`{"key":"val"}`, []string{`json.key = "val"`}},
{`{"key":"val"}`, []string{`json.key "val"`}},
{`{"!@#$%^&*()-_=+|{}привет,<>/?":1,"x x":2,"x][x":3,"x.x":4}`, []string{
`json.!@#$%^&*()-_=+|{}привет,<>/? = 1`,
`json["x x"] = 2`,
`json["x][x"] = 3`,
`json["x.x"] = 4`,
`json.!@#$%^&*()-_=+|{}привет,<>/? 1`,
`json["x x"] 2`,
`json["x][x"] 3`,
`json["x.x"] 4`,
}},
{`{"key":{"key":"val"}}`, []string{`json.key.key = "val"`}},
{`{"key":["val"]}`, []string{`json.key[0] = "val"`}},
{`{"key":{"key":"val"}}`, []string{`json.key.key "val"`}},
{`{"key":["val"]}`, []string{`json.key[0] "val"`}},
{
`{"key":[{"key":"val"},{"key":["val"]},42]}`,
[]string{
`json.key[0].key = "val"`,
`json.key[1].key[0] = "val"`,
`json.key[2] = 42`,
`json.key[0].key "val"`,
`json.key[1].key[0] "val"`,
`json.key[2] 42`,
},
},
{
`[{"key":["val",42,null]}]`,
[]string{
`json[0].key[0] = "val"`,
`json[0].key[1] = 42`,
`json[0].key[2] = null`,
`json[0].key[0] "val"`,
`json[0].key[1] 42`,
`json[0].key[2] null`,
},
},
{
`[1,"hi",true,null,{},[]]`,
[]string{
`json[0] = 1`,
`json[1] = "hi"`,
`json[2] = true`,
`json[3] = null`,
`json[0] 1`,
`json[1] "hi"`,
`json[2] true`,
`json[3] null`,
},
},
}
Expand Down
Loading

0 comments on commit 6e04388

Please sign in to comment.