Transform String to Number with Conditions

Hello Team,

I’m using Grafana Cloud with Infiniti API to get some information from Network device.
I would like to create a time series graph of the interface status but the API answer is a string, Active, Ready, Failed. How can I change these three results to numbers to customize a graph?

The output in the table format

image

Would be good to see Active = 2, Ready =1, Other = 0

Thanks in advance,
Cassio Gomes

please provide sample json data coming from the api

Hi,
Please find below:

[{“gateway”:“192.168.18.1”,“interface”:“wan1”,“ip”:“192.168.18.100”,“ipAssignedBy”:“static”,“primaryDns”:“8.8.8.8”,“publicIp”:“X.X.X.X”,“secondaryDns”:“1.1.1.1”,“status”:“ready”},

{“gateway”:“191.185.32.1”,“interface”:“wan2”,“ip”:“191.185.33.58”,“ipAssignedBy”:“dhcp”,“primaryDns”:“181.213.132.2”,“publicIp”:“Y.Y.Y.Y”,“secondaryDns”:“181.213.132.3”,“status":"active”}]

I’m interested in the field Interface and status.

Thanks in advance,

Cassio Gomes

1 Like

one possible approach using jsonata UQL

1 Like

Yeah.
Could you please write the complete sentence in text format here.
I’ll try right now
Thank you very much
Cassio Gomes

purposely did not do that so you do not copy/pasta. important to understand the code :wink:

I tried but I got only 0

parse-json
jsonata “($ ~> | $ | {‘new_status’: status0 = ‘ready’ ? 1 : status0 = ‘active’ ? 2 : 0} |)”

Cassio Gomes

compare your jsonata with the json data you posted.

OK, I could make it work when i have just one line.

[{“gateway”:“192.168.18.1”,“interface”:“wan1 ”,“ip”:“192.168.18.100”,“ipAssignedBy”:“static”,“primaryDns”:“8.8.8.8”,“publicIp”:“X.X.X.X”,“secondaryDns”:“1.1.1.1”,“status”:“ready” },

But when I have two lines in the same device, it is not working.

This is the original API answer. Inside of uplinks field, have a json with one answer and for some devices two answers. Would be possible use an array format?

This below syntax is working just for one line
jsonata “($ ~> | $ | {‘new_status0’: uplinks.status = ‘ready’ ? 1 : uplinks.status = ‘active’ ? 2 : 0} |)”

I tried
jsonata “($ ~> | $ | {‘new_status0’: uplinks.status[1] = ‘ready’ ? 1 : uplinks.status[1] = ‘active’ ? 2 : 0} |)”
But did not work

Cassio Gomes

please create a new question as your requirements keep changing