How to parse an escaped json string with ansible/jmespath/jq?
2
votes
1
answer
3845
views
I'm using the Ansible module for Bluecat to make an authorized API call to get some information about a subnet. The response looks something like this:
"result": {
"changed": false,
"failed": false,
"json": "b'{\"id\":12345,\"name\":\"SUBNET NAME\",\"properties\":\"CIDR=10.2.2.0/24|allowDuplicateHost=enable|pingBeforeAssign=disable|inheritAllowDuplicateHost=true|inheritPingBeforeAssign=true|gateway=10.2.2.1|inheritDNSRestrictions=true|inheritDefaultDomains=true|inheritDefaultView=true|\",\"type\":\"IP4Network\"}\\n'",
"msg": "",
"status": 200
}
As you can see, all the useful data is in that json
field, but it's some string literal abomination with escaped quotes and newlines. If I run
- debug:
msg: "{{ result | json_query('json.name') }}"
in Ansible, it gives me back the msg
field instead! I can get the entire json
field, but not anything inside it. If I tinker with it a little bit and trim the b
at the beginning, the inner single quotes, and the extra backslash by the newline at the end, then jq .json | fromjson
parses it correctly. But I'm fairly certain b''
just means byte encoding and shouldn't break the parsing, but it does. And what's with the double backslashes at the end?
Do I have any options beyond using some sed
black magic to wipe out all of the escape characters? Why would a web API return a string literal like this?
Asked by Sam Bishop
(25 rep)
May 12, 2022, 11:13 PM
Last activity: May 16, 2022, 11:38 PM
Last activity: May 16, 2022, 11:38 PM