Skip to content

Instantly share code, notes, and snippets.

@sudocurse
Forked from fernandoacorreia/jsonpaths
Last active April 29, 2020 10:30
Show Gist options
  • Save sudocurse/baac2fe9afebfc4a0488ccc2bcc680a4 to your computer and use it in GitHub Desktop.
Save sudocurse/baac2fe9afebfc4a0488ccc2bcc680a4 to your computer and use it in GitHub Desktop.

Revisions

  1. sudocurse revised this gist Apr 29, 2020. 1 changed file with 29 additions and 3 deletions.
    32 changes: 29 additions & 3 deletions uncatj
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,34 @@
    # back to json.
    # The newline in the middle of the sed command is for BSD compatibility
    #
    # Still some problems:
    # Still one problem:
    # - top-level arrays flatten to "[0].asdf" which cause invalid path expressions
    # The only hack I can think of right now for this is to wrap top level arrays
    # as a value in a pair in the top level object:
    # sed -E 's/(^\[[0-9]\])/.TLA\1/g'
    # along with the <<< '{"TLA":[]}' listed below instead of <<< '{}'
    #
    # Doing so would produce the following:
    # % echo '["g", 3, "throw"]' | catj | sed -E 's/(^\[[0-9]\])/.TLA\1/g' | uncatj
    # {
    # "TLA": [
    # "g",
    # 3,
    # "throw"
    # ]
    # }
    # This ugly change also forces an extra element into normal objects:
    #
    # % echo '.HostConfig.PortBindings.HostIp = ""' | uncatj
    # {
    # "TLA": [],
    # "HostConfig": {
    # "PortBindings": {
    # "HostIp": ""
    # }
    # }
    # }
    # I hate it but it works

    ( jq -r "$(sed 's/$/ |/;$a\
    .')" <<< '{}' ) < "${1:-/dev/stdin}"
    ( jq -r "$(sed -E 's/(^\[[0-9]\])/.TLA\1/g ; s/$/ |/;$a\
    .')" <<< '{"TLA":[]}' ) < "${1:-/dev/stdin}"
  2. sudocurse revised this gist Apr 29, 2020. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion uncatj
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1 +1,16 @@
    #!true
    #!/bin/bash
    #
    # Converts flat json such as:
    #
    # .clients[0].id = "59761c23b30d971669fb42ff"
    # .clients[0].isActive = true
    # .clients[0].age = 36
    #
    # back to json.
    # The newline in the middle of the sed command is for BSD compatibility
    #
    # Still some problems:
    # - top-level arrays flatten to "[0].asdf" which cause invalid path expressions

    ( jq -r "$(sed 's/$/ |/;$a\
    .')" <<< '{}' ) < "${1:-/dev/stdin}"
  3. sudocurse revised this gist Apr 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion catj
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ jq -r --stream '
    .[0] | map(
    if type == "number"
    then "[" + tostring + "]"
    else "." + .
    else "." + "\"" + . + "\""
    end
    ) | add
    ) + " = " + (.[1] | @json)
  4. sudocurse revised this gist Apr 29, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions uncatj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #!true
  5. sudocurse revised this gist Apr 29, 2020. 2 changed files with 1 addition and 21 deletions.
    2 changes: 1 addition & 1 deletion catj
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ jq -r --stream '
    end
    ) | add
    ) + " = " + (.[1] | @json)
    '
    ' < "${1:-/dev/stdin}"
    20 changes: 0 additions & 20 deletions jsonpaths
    Original file line number Diff line number Diff line change
    @@ -1,20 +0,0 @@
    #!/bin/bash
    #
    # Outputs JSON paths.
    #
    # Based on https://news.ycombinator.com/item?id=20264654
    #
    # Usage:
    # curl -s https://raw.githubusercontent.com/sitepoint-editors/json-examples/master/src/db.json | ./jsonpaths

    jq -r --stream '
    select(length > 1)
    | (
    .[0] | map(
    if type == "number"
    then "[" + tostring + "]"
    else "." + .
    end
    ) | add
    ) + " = " + (.[1] | @json)
    '
  6. sudocurse revised this gist Apr 29, 2020. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions catj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/bash
    #
    # Outputs JSON paths.
    #
    # Based on https://news.ycombinator.com/item?id=20264654
    #
    # Usage:
    # curl -s https://raw.githubusercontent.com/sitepoint-editors/json-examples/master/src/db.json | ./jsonpaths

    jq -r --stream '
    select(length > 1)
    | (
    .[0] | map(
    if type == "number"
    then "[" + tostring + "]"
    else "." + .
    end
    ) | add
    ) + " = " + (.[1] | @json)
    '
  7. @fernandoacorreia fernandoacorreia revised this gist Jun 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jsonpaths
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    #
    # Displays JSON paths.
    # Outputs JSON paths.
    #
    # Based on https://news.ycombinator.com/item?id=20264654
    #
  8. @fernandoacorreia fernandoacorreia created this gist Jun 29, 2019.
    20 changes: 20 additions & 0 deletions jsonpaths
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/bash
    #
    # Displays JSON paths.
    #
    # Based on https://news.ycombinator.com/item?id=20264654
    #
    # Usage:
    # curl -s https://raw.githubusercontent.com/sitepoint-editors/json-examples/master/src/db.json | ./jsonpaths

    jq -r --stream '
    select(length > 1)
    | (
    .[0] | map(
    if type == "number"
    then "[" + tostring + "]"
    else "." + .
    end
    ) | add
    ) + " = " + (.[1] | @json)
    '