jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.33k stars 1.57k forks source link

Is it possible to output the entire JSON input with matches highlighted / colorized? #1925

Open vaibhav101 opened 5 years ago

vaibhav101 commented 5 years ago

Example

Input

{
  "test": [
    { "key1": "val1", "key2": "val2"},
    { "key1": "val3", "key2": "val4"}
  ]
}

Selector

.test[].key2

Output

{
  "test": [
    { "key1": "val1", "key2": **"val2"**},
    { "key1": "val3", "key2": **"val4"**}
  ]
}

My use case is to create a Sublime Text plugin which selects the matching regions.

Thanks!

wtlangford commented 5 years ago

The output you're looking for isn't valid json, so it's not possible (and won't be) with jq.

nicowilliams commented 5 years ago

@wtlangford, I suspect that @vaibhav101 means that in colorized output mode the parts @vaibhav101 highlighted with asterisks would actually be highlighted with color. I think that would be OK, because remember, colorized JSON output isn't valid JSON either :)

However, I don't really know how to implement something like this in a sensible way.

vaibhav101 commented 5 years ago

Hey @nicowilliams, you got it right. I want to highlight the matching portions of JSON.

If this feature does not exist, can you point me towards how to go about creating it?

Thanks!

nicowilliams commented 5 years ago

@vaibhav101 it's... not easy. The problem is that jq is not just a .this[0].that path expression language only -- it's a full-blown programming language, and while path expressions are essential to jq's nature, you can have programs that aren't path expressions, and you can have path expressions embedded in several subexpressions. So, when we're matching a path, that doesn't mean we're matching a path that the user wants highlighted -- the path matched might not even exist in the final output object.

Also, the jv API used internally for representing parsed values doesn't currently have a way to say "hey, highlight this on output in colorized output mode". Changing this would be the first step, but then that would have to be integrated into the language somehow.

Assuming jv support for highlighting, the next step would probably be to add a builtin function that takes a path expression and marks the matching values (if any) to be highlighted.

If you want to take a crack at this, my advice would be to start with this reading list, in this order:

Don't be intimidated. That's a lot of stuff, though as programming languages go, it's pretty small.