SOLVED How to mangle the API Information with jq (selected:1)

Started by NorbertK, April 17, 2024, 03:19:48 PM

Previous topic - Next topic
Hello all,
i want to document my configuration with the help of the web API but have some problems with the JSON output.
The API-Endpoints deliver JSON with objects with a lot of attributes, but only one of them is relevant.

Consider the following snippet from  $URL/unbound/settings/get ...

  "hosts": {
    "host": {
      "6de7d5b1-988e-4b69-80e7-1f64198d1800": {
        "enabled": "1",
        "hostname": "fileserver",
        "domain": "fm174.intern",
        "rr": {
          "A": {
            "value": "A (IPv4 address)",
            "selected": 1
          },
          "AAAA": {
            "value": "AAAA (IPv6 address)",
            "selected": 0
          },
          "MX": {
            "value": "MX (Mail server)",
            "selected": 0
          }
        },
        "mxprio": "",
        "mx": "",
        "server": "10.20.60.12",
        "description": "Filserver"
      },


The JSON contains information for all kinds of "rr", but only the "A" record is important here. This can easily be seen because the '"selected": 1' clause.

This structure is a kind of idiom in the OPNSense world and I find it hard to handle. How can I mangle this output in a way that I see only the relevant and true information ? Bonus points for using "jq"  ;)

Did anyone try something like that already ?

Thanks for reading!

Norbert
Kind regards and thanks !

Norbert

Sorry, the JSON I pasted was not valid. Thanks to substack I got the solution.

Given a valid JSON snippet we can do

.hosts.host |to_entries |.[].value["rr" ][] |= select(.selected == 1)


Details see here :https://jqplay.org/s/rZxB3W0tMB_b

The substack answer is here https://stackoverflow.com/a/78340508/153578
Kind regards and thanks !

Norbert