I am implementing a program accessing a REST API, which can be filtered server-side by passing in a jq-statement. My issue is with writing a jq filter that includes an element called "forecast.forecast" in the output only if it has content in the input, and also filters the content.
Here is an example of the input with forecast being present:
{"result":{"forecast":{"solar":{"scale":0.5792918589105848,"today":{"energy":21142.72731634481,"complete":true },"tomorrow":{"energy":36320.01044880075,"complete":true },"dayAfterTomorrow":{"energy":12222.739005817713,"complete":false },"timeseries":[ {"ts":"2025-03-12T00:00:00+01:00","val":0 } ] } },"siteTitle":"KOG" }}
Here is an example with forecast being empty:
{"result":{"forecast":{},"siteTitle":"KOG" }}
The "forecast" element may also be not present at all:
{"result":{"siteTitle":"KOG" }}
My goal is to get only the "scale" and "energy" values from "forecast.solar", and also other elements on the same level as "forecast", such as "siteTitle".
Here an example that gives me the "siteTitle" and the "scale":
{result:{siteTitle:.siteTitle,forecast:{solar:{scale:.forecast.solar.scale}}}}
However, even if the "scale" value is not present in the input, the output contains it as null:
{"result":{"forecast":{"solar":{"scale":null } },"siteTitle":"KOG" }}
I already tried using select to check if "forecast.solar" is present, but with what I came up with, the complete result is empty when "forecast.solar" is not present.
jq={result:{siteTitle:.siteTitle,forecast:(.forecast|select(.solar?)|{solar:{scale:.solar.scale}})}}
This returns:
jq: empty result
How does the statement need to look like to include fields like the "siteTitle", even if "forecast.solar" is not present?