2

How does one go about contructing the path to a deeply nested field from a large json document.

The approach I've typically used is to flatten the JSON using a jq expression.

map
(
    with_entries(select(.key != "fields"))
    +
    (.fields | with_entries(.value = .value[0]))
)

However, I'm preparing for a practical test, where jq will not be available. I've reverted to scrolling up and down with less to figure out document structure.

Is there an easier way to do this with standard Linux tools?

  • 1
    vim with a JSON filetype and some folding perhaps, but I sense that's not really in the spirit of the task you've been given. – eleventyone Aug 15 '20 at 12:39
  • That's a great idea. Vim is universally present, and should be in the lab environment. I just need to do some research on what is needed to support json folding in vim. Hopefully it doesn't take a lot of config or plugin installation from internet. – Kshitiz Sharma Aug 15 '20 at 14:27
  • Though not among the core tools I'm afraid jq is the standard Linux tool to handle json. vim's own syntax folding abilities can certainly be better than a plain less over the document but I suspect it is available only on full vim, not on vim-tiny which is often the default on e.g. Ubuntu systems. Will you be allowed to use your own script on (or perhaps copy&paste from/to) that lab's linux vanilla system? – LL3 Aug 17 '20 at 14:59
  • @LL3 With vim you can use :set foldmethod=syntax and create folds with za. Works quite well to drill down/out of a json. You're right it will be tricky if it doesn't have full vim. Which is quite likely given that test is on Ubuntu 16 Server edition. You're not allowed to open any internet sites during exam. It doesn't say anything about installing packages, but I'm not willing to risk it. Worst case, I think I'll use Python 2.7 json parsing. – Kshitiz Sharma Aug 17 '20 at 20:44

1 Answers1

0

Sounds like you mean visually rather than programatically, in which case an editor with support for working with JSON might work. For example vim with a JSON filetype handler and its built-in folding support.