fd icon indicating copy to clipboard operation
fd copied to clipboard

Request for `--json` flag

Open xpe opened this issue 4 months ago • 7 comments

This would help people who like:

  • jq
  • nushell

Thanks!

xpe avatar Aug 03 '25 20:08 xpe

Do you have a schema in mind for the output? Just an array of the file names?

tmccombs avatar Aug 03 '25 23:08 tmccombs

Hi. I recommend an array of objects. This allows the output to be predictable even with different command flags that change which columns are output; e.g. fd and fd -l.

Here are some examples from nushell (in reverse: telling it to generate JSON):

4 columns from ls in nu

#!/usr/bin/env nu
ls App* | to json
[
  {
    "name": "Applications",
    "type": "dir",
    "size": 194,
    "modified": "2025-07-28 08:01:22.259823 -04:00"
  }
]

13 columns from ls -l in nu

#!/usr/bin/env nu
ls -l App* | to json
[
  {
    "name": "Applications",
    "type": "dir",
    "target": null,
    "readonly": false,
    "mode": "rwxr-xr-x",
    "num_links": 5,
    "inode": 29820936,
    "user": "person",
    "group": "wizard",
    "size": 194,
    "created": "2023-12-20 10:26:15.718489 -05:00",
    "accessed": "2025-07-30 09:04:44.061736 -04:00",
    "modified": "2025-07-28 08:01:22.259823 -04:00"
  }
]

1 column from fd

#!/usr/bin/env zsh
fd --json App* # proposed

Only showing one result for brevity...

[
  {
    "name": "Applications"
  }
]

xpe avatar Aug 04 '25 02:08 xpe

The -l option just delegates to the ls executable, fd itself doesn't generate the fields.

tmccombs avatar Aug 04 '25 02:08 tmccombs

Ah, this makes it more interesting. 🤔

  • From one point of view, it is reasonable to expect fd to guarantee its own output format.

  • From another point of view -- like what you said (and I looked at the code) -- fd delegates to the OS-specific ls command, which is useful because it keeps the output familiar to the user.

What do you think?

xpe avatar Aug 06 '25 13:08 xpe

I like this idea. I would definitely use a --json option if it were available.

scottchiefbaker avatar Sep 01 '25 23:09 scottchiefbaker

Array of objects is not really ideal for streaming. I think json-lines / json-seq would be better.

Cubixmeister avatar Nov 25 '25 10:11 Cubixmeister

@Cubixmeister yes, the PR now uses jsonl as output

tmccombs avatar Nov 25 '25 16:11 tmccombs