kennknowles / python-jsonpath-rw

A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.
Apache License 2.0
599 stars 193 forks source link

How do I run a new find() action from a jsonpath reference? #50

Closed larryfast closed 6 years ago

larryfast commented 7 years ago

I'm trying to search through a facter-ansible-json report on my servers. Periods in the fqdn's are messing up the jsonpath constructs. I may be using the wrong mechanic for incremental search. The method full_path may have options to work around this. Or ...???

Deconstructed, here's the problem.

json_src = """ { "hostvars": { "server_XYZ.my_domain.net": { "facter_hostname": "server_XYZ", "facter_OS": "CentOS", "module_setup": true, "facter_uptime_hours": 984, "ansible_distribution_version": "7.2.1511", "facter_blockdevice_fd0_size": 4096, }, { "server_ABC.my_domain.net":{ "facter_hostname": "server_ABC", } } """ The code below fails because periods in the fqdns invalidate the jsonpath syntax.

path1 = parse("$.hostvars.*") matches1 = path1.find(json_src) path2 = parse( str(matches1[0].full_path) + ".facter_OS") produces "$.hostvars.server_XYZ.my_domain.net.facter_OS" => FAIL

Q1: My code doesn't feel right and I expect I'm missing something obvious. Is there a more appropriate construct for using the match records as the foundation for my next search?

Q2: Does full_path() have options to produce the safer alternate syntax: "$.['hostvars'].['server_XYZ.my_domain.net'].facter_OS"

Q3: Any other ideas?