Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active January 25, 2022 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryuheechul/6a1ce296873a5d97ef63f137fedfd1de to your computer and use it in GitHub Desktop.
Save ryuheechul/6a1ce296873a5d97ef63f137fedfd1de to your computer and use it in GitHub Desktop.

What is this?

Something like Useful commands for jq

Useful tools

Parse escaped json as a field

input

[
    {"a": 1, "e": "{\"hi\": \"there\"}"},
    {"b": 2, "e": "{\"hi\": \"there\"}"}
]

output

jq '[.[] | .e |= fromjson]'

[
  {
    "a": 1,
    "e": {
      "hi": "there"
    }
  },
  {
    "b": 2,
    "e": {
      "hi": "there"
    }
  }
]

explain

  • [ .[] ( do something here... ) ]: select each item in the list and put it back into a list
  • | .e |= fromjson: apply fromjson filter and replace the value

Unique

unique_by

. | unique_by (.)

Add lengths

input

{
    "items": [ "..." ]
}

output

jq '. + {"length": (.items | length)}'
{
    "items": [ "..." ],
    "length": 1
]

Filter by fields

| { field, name }

Copy

. + {"copied": .fromOriginal }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment