4

Json file sample (test1.json) :

{
  "entries": [
    {
      "duration": 478,
      "id": "uCPPyitzI",
      "title": "Why | TV",
      "description": null,
      "uploader": null,
      "view_count": null,
      "ie_key": "Youtube",
      "url": "uCPPyitzI",
      "_type": "url_transparent"
    },
    {
      "duration": 245,
      "id": "F9ClW8Dv0",
      "title": "Kejrixxxx | TV",
      "description": null,
      "uploader": null,
      "view_count": null,
      "ie_key": "Youtube",
      "url": "F9ClW8Dv0",
      "_type": "url_transparent"
    },
    {
      "duration": 990,
      "id": "JchBXwZ8o",
      "title": "Battle Of xxxx: | Dr. cccccc | TV",
      "description": null,
      "uploader": null,
      "view_count": null,
      "ie_key": "Youtube",
      "url": "JchBXwZ8o",
      "_type": "url_transparent"
    },
    {
      "duration": 1178,
      "id": "4x9dWcG2c",
      "title": "Dirty Politics | Dr. xxxxx | TV",
      "description": null,
      "uploader": null,
      "view_count": null,
      "ie_key": "Youtube",
      "url": "4x9dWcd2c",
      "_type": "url_transparent"
    }
  ],
  "uploader_id": "xxxx",
  "webpage_url": "https://www.youtube.com/playlist?list=xxx",
  "title": ".aabbcc",
  "extractor": "xxx:xxx",
  "uploader": "xxxxxx",
  "uploader_url": "https://www.youtube.com/channel/xxxx",
  "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "extractor_key": "xxxxx",
  "webpage_url_basename": "xxxxxx",
  "_type": "playlist"
}

The following command works fine in Linux:

cat test1.json | jq -r '.entries | map(.id +" | "+ .title) | join("\n")'

As per jq documentations :

When using the Windows command shell (cmd.exe) it's best to use double quotes around your jq program when given on the command-line (instead of the -f program-file option), but then double-quotes in the jq program need backslash escaping.

So I modified the command to for cmd.exe (Windows 10):

cat test1.json | jq -r ".[] | map(.id +\" | \" + .title) | join(\" \n \")"

But I keep getting the following error :

'\" + .title) | join(\"' is not recognized as an internal or external command,
operable program or batch file.

What am I doing wrong? Please help.

Neel
  • 1,901
  • 4
  • 19
  • 31
  • The [`cat` is useless](https://stackoverflow.com/questions/11710552/useless-use-of-cat) on Unix, too. – tripleee Dec 16 '20 at 06:48

1 Answers1

4

Unfortunately the jq documentation in trying to be helpful about Windows ends up being a bit misleading. Perhaps it would have been better to provide a pointer to comprehensive documentation, with the suggestion to avoid the headaches by using the -f command-line option.

One of the important details that is missing is that the caret character ^ serves as an escape character for symbols such as |. So you could write:

 .entries ^| map(.id +^" ^| ^"+ .title) ^| join(^"\n^")

without any surrounding quotes.

Alternatively - and a bit more mysteriously - you could write:

".entries | map(.id +\" ^| \"+ .title) | join(\"\n\")"

As you can infer from the fact that not all pipe characters in the above line are escaped, this technique is not so simple. For more detailed information, see for example this SO article: Escape double quotes in parameter

or:

https://www.robvanderwoude.com/escapechars.php

For an online tool, see http://output.jsbin.com/anitaz/11

peak
  • 105,803
  • 17
  • 152
  • 177