Workaround for "fields have different lengths" #250
Replies: 11 comments 2 replies
-
I'm battling with the same issue and it makes this plugin pretty problematic on a dataset that allows missing fields (which is common on JSON APIs). Is this issue here by design? Simply using the empty string in this case seems like it would make a lot more sense. Or allow it as an option at least. |
Beta Was this translation helpful? Give feedback.
-
I'm running into this myself from time to time, so I'm definitely aware of the struggle here. The problem right now is that the JSON Path queries for each field are run independently of each other. I'd love if JSONPath had supported setting defaults or something similar. I have some ideas on how to solve it. For example, the JSONPath library seem to allow you to run a callback for the values, which could help to solve this. Another idea I've had is to support nested JSONPaths where you could define a default value if a nested query returns empty results. This will be supported as soon as we figure out a reasonable way to do it :) |
Beta Was this translation helpful? Give feedback.
-
I'm not familiar with grafana development but I took a quick look at the code and can't this be fixed by simply adjusting the empty array return from JSONPath? Since you're just trying to ensure the generated DataFrame is valid, equating no return from JSONPath with the empty string seems like it should be a valid thing to do and more usable than throwing an error. Something simple like:
|
Beta Was this translation helpful? Give feedback.
-
Spent a few minutes to figure out how to run the plugin in dev mode and I see the issue now. The return value from JSONPath is already a full array and you completely lose the record alignments in the DataFrame. It also looks like even if you use the callback to JSONPath it doesn't appear to actually call it when the field is completely missing. |
Beta Was this translation helpful? Give feedback.
-
It looks like this likely requires a change to the underlying JSONPath-Plus library. The other JSONPath lib (https://github.com/json-path/JsonPath) has an option I went to open a ticket on JSONPath-Plus and realized that project is now un-maintained so I definitely get why you hit a wall with this. |
Beta Was this translation helpful? Give feedback.
-
Yes, that's better explanation than mine :)
Thank you for taking the time to look into this 🙏 One of my plans for the next major release is to rewrite it into a backend plugin (in Go) to support alerting. The tricky thing will be to find a JSONPath library for Go. Unfortunately, switching to any other library will likely make it hard to maintain feature parity. I'll be somewhat unavailable this week, but I'm planning to spend the next two weeks of my vacation on plugin work, I might take the opportunity to start working on this then. |
Beta Was this translation helpful? Give feedback.
-
I think (hope, guess) I have found a workaround. For me at least it works around the issue of missing fields. Maybe it helps others, too. Let me know how universal that is. The trick for me was to use JSONata instead of JSONPath for the affected fields. Took a little bit of time to get used to it, but I have a few issues sorted out now. Two features in JSONata seem important to me:
Here's a very basic JSONata expression that does it for me with the field "h" (for "hours")
Explanation:
Hope this helps someone |
Beta Was this translation helpful? Give feedback.
-
Hello, very interesting discussion. Many thanks, Sebastian |
Beta Was this translation helpful? Give feedback.
-
Maybe I misunderstand the question, but I'm pulling data from openweathermap.org and I have (truncating) this as my JSON:
The thing is, only if there's rain, this object is present:
I can't make it work with JSONPath as a field, but this JSONana ternary seems to do the trick:
It hasn't rained since I started this project, but I've tested it replacing rain."1hr" with wind.gust and it seems to work fine. |
Beta Was this translation helpful? Give feedback.
-
I have the same issues with weather data from https://donneespubliques.meteofrance.fr/?fond=produit&id_produit=90&id_rubrique=32
Yes, it is not really beautiful. Grafana is able to handle null values instead of number, so I would have expected the same here 😉 |
Beta Was this translation helpful? Give feedback.
-
Hi It feels like I have tried everything but I cant figure out how to do it.... Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi, I understand what is meant by Fields have different lengths, but is there any workaround to avoid this without losing any array elements by coalescing missing values to some default? It's a critical requirement for us.
For example, form the following input:
I want to get
["order-api", "billing-api"]
forname
field,["1", ""]
forversion
field.Beta Was this translation helpful? Give feedback.
All reactions