Search code examples
json-c

extract key (not value) from json-c json_object


I am currently playing with libnftables and json-c (C libraries) my aim is to parse nftables rules in json format with json-c library

The thing is there, nftables elements (a table for example) look like this:

{ "table":{
            "family":"ip",
            "name":"filter",
            "handle":6

           }

}

A nftables table is basically a json_object structure with the key "table" and another json_object structure as value storing all other informations. Chains and rules are similar but with different key

{"chain":{
        "family":"ip",
        "table":"filter",
        "name":"INPUT",
        "handle":1,
        "type":"filter",
        "hook":"input",
        "prio":0,
        "policy":"accept"

        }

},

I need a way to retrieve the key (like "table", "rule" , "chain") from a json_object since the whole nftables rules are a mixture of many things making it difficult to guess the kind of nftables elements we are dealing with when browsing through the array of nftables rules.

Thank you!


Solution

    • use libnftables in json mode to retrieve ruleset in json object

    • get a good json parsin library (I recommend libjansson) to parse json object in order to retrieve informations

    NB: you might have to compile nftables to be able to use json output format.