Jsonata UQL query not working - API: error applying uql query

Using Grafana version 11.2.2

I am trying to use a UQL query to modify my JSON data returned by an API query. I am using a UQL query as follows.

parse-json

| jsonata $map(content.rows,function($row){$map($row,function($cell,$index){$index = 2 and $cell[0] = "BE4" ? ["EMEA"] : $cell})})

Against the following JSON

{
	"content": {
		"columns": [
			"Device",
			"IP Address",
			"Instance"
		],
		"rows": [
			[
				[
					"condarsbe08.acme.com"
				],
				[
					"10.10.210.213"
				],
				[
					"BE4"
				]
			],
			[
				[
					"condarsbe15.acme.com"
				],
				[
					"10.10.210.220"
				],
				[
					"BE3"
				]
			],
			[
				[
					"condarsbe04.acme.com"
				],
				[
					"10.10.210.209"
				],
				[
					"BE4"
				]
			],
			[
				[
					"condarsbe13.acme.com"
				],
				[
					"10.10.210.218"
				],
				[
					"BE3"
				]
			],
			[
				[
					"condarsbe06.acme.com"
				],
				[
					"10.10.210.196"
				],
				[
					"BE4"
				]
			],
			[
				[
					"condarsbe02.acme.com"
				],
				[
					"10.10.210.207"
				],
				[
					"BE1"
				]
			],
			[
				[
					"condarsbe03.acme.com"
				],
				[
					"10.10.210.208"
				],
				[
					"BE2"
				]
			],
			[
				[
					"condarsbe06.acme.com"
				],
				[
					"10.10.210.196"
				],
				[
					"BE1"
				]
			],
			[
				[
					"condarsbe08.acme.com"
				],
				[
					"10.10.210.213"
				],
				[
					"10.10.210.220"
				],
				[
					"BE1"
				]
			],
			[
				[
					"condarsbe11.acme.com"
				],
				[
					"10.10.210.216"
				],
				[
					"BE4"
				]
			],
			[
				[
					"condarsbe08.acme.com"
				],
				[
					"10.10.210.213"
				],
				[
					"BE2"
				]
			]
		]
	},
	"id": "0-t62ce-6937c556",
	"name": "Backends",
	"path": "All/Report Library/System Health/Inventory/Backends",
	"properties": {},
	"time-range": {
		"end": 1743014107,
		"start": 1742927707
	},
	"type": "table"
}

The query has been tested in JSONata exerciser and works as expected. See screen shot highlighted in yellow, the text was originally BE4 and have been changed to “EMEA”, as required

The end result is to have all of the Instance values, BE1, BE2, BE3, BE4, updated to read as EMEA. Im only testing for BE4 in this example.

However when I run this as a UQL query it comes back with the errors saying ‘error applying uql query’. I think this is a syntax issue but im lost and need help. I am creating this data source as a mixed data source as I need to call multiple sources and join the results together. In this example I am only getting the data from one API call.

parse-json
| jsonata "(your_foo)"

https://try.jsonata.org/5jRhKTf0z

Thanks for reply.

I see the same data/results in your query as in mine. What I’m looking to modify is every entry within the ‘Instance’ Column with a new string. Instead of each row in the instance columns reading BE1 or BE2 or BE3 etc. I just want to replace it with a new string for all. I.e “EMEA”.

Thanks

Hi,

If I take your query and update as follows. I get the results I want but this does not run in the UQL query window in Grafana.

$.content.rows.{
‘device’: $[0],
‘ip’: $[1],
‘instance’: $[2] = “BE3” or “BE4” or “BE1” or “BE2”? [“EMEA”]
}

Why does this not work in Grafana UQL ? Is this a syntax issue, is there is bug or an im not understanding how this should work ?

Thanks

What does the jsonata look like in grafana that is not working

Here is the jsonata -

parse-json
| jsonata “($.content.rows.{
‘device’: $[0],
‘ip’: $[1],
‘instance’: $[2] = “BE3” or “BE4” or “BE1” or “BE2”? [“EMEA”]
})”

I’ve also tried putting it all on one line

parse-json

| jsonata “($.content.rows.{‘device’: $[0],‘ip’: $[1],‘instance’: $[2] = “BE3” or “BE4” or “BE1” or “BE2”? [“EMEA”]})”

Error both times is - API: error applying uql query

Thanks

Look at my jsonata. It has to be one line and you need to use single quotes within the jsonata

Thanks for the single quote tip. I’m now getting data back but the BE values are not converted to EMEA

parse-json

| jsonata “($.content.rows.{‘device’: $[0],‘ip’: $[1],‘instance’: $[2] = ‘BE1’ or ‘BE4’ or ‘BE1’ or ‘BE2’ ? [‘EMEA’]}))”
image

check the docu out for jsonata

Conditional ternary operator x ? yes : no

Boolean operator

The problem is not the operator but the use of double quotes, my json results are [“BE1”]. I cannot find a way to run the expression with double quotes, which is what is needed to match the value in the array.

check this out, it seems like you have both wrong boolean and conditional ternary operators

$.content.rows.{
    'device': $[0],
    'ip': $[1],
    'instance': [$[2] = "BE4" or $[2] = "BE3"] ? 'EMEA' : $[2],
    'wrong_boolean_and_ternary_operator': [$[2] = "BE3" or "BE2" or "BE1"] ? 'Vader' : 'EMEA'
}