Parsing nested JSON to grab target values

I am getting a raw JSON response from an external API. There is a lot of unneeded and complex data in the external API. I am looking to streamline it down into a much smaller object or array with only the relevant data with a function. I am struggling to find a way to do this... suggestions?

(as a bonus, there is a possibility not all subcats will have a value. I would want to skip these instead of having to filter_empty later)

JSON Input

{
    "cat1": {
        "subcat1": {
            "metadata": {},
            "targetvalue": "thisValue1",
            "timestamp": "2024-05-02"
        }
    },
	"cat2": {
        "subcat2a": {
            "metadata": {},
            "targetvalue": "thisValue2",
            "timestamp": "2024-05-02"
        },
		"subcat2b": {
            "metadata": {},
            "targetvalue": "thisValue3",
            "timestamp": "2024-05-02"
        }
    },
	"catN": {
        "subcatN": {
            "metadata": {},
            "targetvalue": "thisValueN",
            "timestamp": "2024-05-02"
        }
    }
}


Target Output

{
	subcat1: thisValue1,
	subcat2a: thisValue2,
	subcat2b: thisValue3,
	subcatN: thisValueN
}

1 reply