1

Is it possible to list/generate all the JSON Pointers from a file using xidel or another command line tool?

I've tried with

xidel test.json -e '$json()'

But it only lists the top-level fields, while i want a recursive listing like the one i get with xmlstarlet el -a.

Community
  • 1
  • 1
eadmaster
  • 1,347
  • 13
  • 23

1 Answers1

1

You can declare a recursive function for that:

xidel test.json -e '
      declare function escape ($s) { replace(replace($s, "~", "~0"), "/", "~1") };
      declare function enum($v, $pointer) {
        typeswitch ($v) 
          case array()  return $v() ! enum(., $pointer || "/" || string(position() - 1))
          case object() return $v() ! enum($v(.), $pointer || "/" || escape(.))
          default       return $pointer
      };
      enum($json, "")
'
BeniBela
  • 16,412
  • 4
  • 45
  • 52
  • Wow, always surprised how powerful and versatile Xidel is. The bots you write are still way above my level. I also found that reddit-chat-bot on your xidel-github, but most of its contents was unfamiliar territory to me. – MatrixView Jun 20 '16 at 11:26
  • I was hoping for a simpler solution, but it works. It also throws errors with [problematic json files like this](https://www.sitepoint.com/google-maps-json-file/). – eadmaster Jul 04 '16 at 17:29