Search code examples
jsonbashzshjq

convert simple json array to plain text


The current command I am using is:

curl -H "Accept: application/vnd.github.full+json" -H "Authorization: 7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b" "https://api.github.com/repos/zsh-users/zsh-syntax-highlighting/git/trees/master?recursive=1" | jq '.tree[]|.path'

I have a return value like

[
  ".editorconfig",
  ".gitattributes",
  ".gitignore",
  ".revision-hash",
  ".travis.yml",
  ".version",
  "COPYING.md",
  "HACKING.md",
  "INSTALL.md",
  "Makefile",
  "README.md",
  "changelog.md",
  "docs",
  "docs/highlighters.md",
  "docs/highlighters",
  "docs/highlighters/brackets.md",
  "docs/highlighters/cursor.md",
  "docs/highlighters/line.md",
  .....
]

I want the return value to be like

.editorconfig
.gitattributes
.gitignore
.revision-hash
.travis.yml
.version
COPYING.md
HACKING.md
INSTALL.md
Makefile
README.md
changelog.md
docs
docs/highlighters.md
docs/highlighters
docs/highlighters/brackets.md
docs/highlighters/cursor.md
docs/highlighters/line.md
....

Solution

  • Use 's -r option (docs):

    jq -r '.tree[]|.path' < <(curl -H "Accept: application/vnd.github.full+json" -H "Authorization: <MY-KEY>" "https://api.github.com/repos/zsh-users/zsh-syntax-highlighting/git/trees/master?recursive=1")
    

    This uses a bash Process Substitution so the interesting part of the answer shows up first. You could write curl ... | jq ...

    Result:

    editorconfig
    .gitattributes
    .gitignore
    .revision-hash
    .travis.yml
    .version
    COPYING.md
    HACKING.md
    ...