Giter VIP home page Giter VIP logo

Comments (7)

mountaineerbr avatar mountaineerbr commented on July 17, 2024 2

I have something that may help in my .bashrc:

#get all paths of a json
jqpath()
{
	 jq -r 'def path2text($value):
		  def tos: if type == "number" then . else tojson end;
		  reduce .[] as $segment ("";  .
		    + ($segment
		       | if type == "string" then "." + . else "[\(.)]" end))
		  + " = \($value | tos)";

		paths(scalars) as $p
		  | getpath($p) as $v
		  | $p | path2text($v)' "$@"
}
jqpath2()
{
	jq -rc 'path(..)|[.[]|tostring]|join("/")' "$@"
	#jq -r '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]' "$@"

}
#https://github.com/stedolan/jq/issues/243
#also see `gron' package to flatten json
#https://www.datafix.com.au/BASHing/2022-03-23.html

from jq.

eostermueller avatar eostermueller commented on July 17, 2024 1

@wader, didn't know about gron, thanks for sharing.
I was thinking this would be much easier. When -p is present, the -r would be implied and the expression mentioned above would be used. Also, I just updated the issue to state that the output jsonpath format should be presentable as input.

from jq.

wader avatar wader commented on July 17, 2024

Hi, interesting idea. I wonder if it could be modelled as another input and output format (#467)? thinking this basically is the gron "format"? example:

$ jq -n '{a:"a",b:[1]}' | gron
json = {};
json.a = "a";
json.b = [];
json.b[0] = 1;

$ jq -n '{a:"a",b:[1]}' | gron | gron -u
{
  "a": "a",
  "b": [
     1
  ]
}

I think there also has been some discussion, that i can't find now, about adding some helpers for dealing paths. That would at least simplify doing things like this a bit, for example fq has path_to_expr and expr_to_path:

$ fq -cn '".a[0]" | expr_to_path | ., path_to_expr'
["a",0]
".a[0]"

from jq.

wader avatar wader commented on July 17, 2024

Agree on output should be in input format, maybe should even be <path> = <value> to be valid jq? not sure what i think about -p yet, have to think about it. It would also be great with input from other jq maintainers on this but it might take a while.

from jq.

pkoppstein avatar pkoppstein commented on July 17, 2024

@wader wrote:

It would also be great with input from other jq maintainers

The jq authors and maintainers seem to be in pretty close agreement that new command-line options are strenuously to be avoided, especially when a straightforward jq program can fill the bill.

In this particular case, the existing --stream option does seem to meet the stated functional requirements. In fact, the JSON stream produced by --stream can be used programmatically even more easily that the proposed output, the point being that the output interfaces nicely with jq's setpath.

Please also note even if the output produced by --stream -c is itself deemed unsatisfactory, it can nevertheless
very easily and succinctly be tweaked to achieve variants such as the one that has been proposed.

from jq.

wader avatar wader commented on July 17, 2024

@wader wrote:

It would also be great with input from other jq maintainers

The jq authors and maintainers seem to be in pretty close agreement that new command-line options are strenuously to be avoided, especially when a straightforward jq program can fill the bill.

Agreed

In this particular case, the existing --stream option does seem to meet the stated functional requirements. In fact, the JSON stream produced by --stream can be used programmatically even more easily that the proposed output, the point being that the output interfaces nicely with jq's setpath.

Please also note even if the output produced by --stream -c is itself deemed unsatisfactory, it can nevertheless very easily and succinctly be tweaked to achieve variants such as the one that has been proposed.

Good point, and it's way more readable then i expected 👍

$ pbpaste | jq --stream -c 'select(length==2)'
[["Reservations",0,"Groups"],[]]
[["Reservations",0,"Instances",0,"ImageId"],"ami-a"]
[["Reservations",0,"Instances",0,"InstanceId"],"i-a"]
[["Reservations",0,"Instances",0,"InstanceType"],"t2.micro"]
[["Reservations",0,"Instances",0,"KeyName"],"ubuntu"]

from jq.

eostermueller avatar eostermueller commented on July 17, 2024

...that's a step in the right direction, thanks for putting that together.

from jq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.