I am trying to get specific datasource row by index.
I have tried =(@ctx.datasources.questions)[0] and =@ctx.datasources.questions[0] without luck. In Jsonata I can do $[0] and it works.
any ideas?
Thanks
I am trying to get specific datasource row by index.
I have tried =(@ctx.datasources.questions)[0] and =@ctx.datasources.questions[0] without luck. In Jsonata I can do $[0] and it works.
any ideas?
Thanks
Hi @adam
Thanks for your question.
The last example you mentioned usually works for me, like in the below scenarios. You will see that I used [0] but also different indexing options like [2] (see the second scenario).
Perhaps you are using it in a different way, feel free to share your YAML with me and I will help you troubleshoot futher. Please also let me know where your data comes from (REST call maybe?).
title: Test Indexing
description: Description of your Jig
type: jig.default
header:
type: component.jig-header
options:
height: medium
children:
type: component.image
options:
source:
uri: https://builder.jigx.com/assets/images/header.jpg
datasources:
mydata:
type: datasource.static
options:
data:
- id: 1
field1: value1
field2: value1
- id: 2
field1: value2
field2: value2
- id: 3
field1: value3
field2: value3
- id: 4
field1: value4
field2: value4
mydataABC:
type: datasource.static
options:
data:
- id: 1
field1: valueA
field2: valueA
- id: 2
field1: valueB
field2: valueB
- id: 3
field1: valueC
field2: valueC
- id: 4
field1: valueD
field2: valueD
children:
- type: component.list
options:
data: =@ctx.datasources.mydata
maximumItemsToRender: 8
item:
type: component.list-item
options:
title: =@ctx.current.item.field1
- type: component.list
options:
data: =@ctx.datasources.mydataABC[0]
maximumItemsToRender: 8
item:
type: component.list-item
options:
title: =@ctx.current.item.field1
stories:
type: component.story-group
options:
actionButtonTitle: Gimme more Deadpool
data: =[@ctx.datasources.videos[19], @ctx.datasources.videos[10], @ctx.datasources.videos[0], @ctx.datasources.videos[1], @ctx.datasources.videos[2]]
# data: =@ctx.datasources.videos[0]
groupName: Deadpool - Because testing has to be FUN!
item:
type: component.video-player
options:
title: =$uppercase(@ctx.current.item.title) &
subtitle: ='Last updated by Chanelle Dicks on 22 February 2023'
thumbnailUri: =@ctx.current.item.thumbnail #="https://i.ytimg.com/vi/" & @ctx.current.item.videoid & "/maxresdefault.jpg"
url: ="https://www.youtube.com/watch?v=" & @ctx.current.item.videoid
onPress:
type: action.go-to
options:
title: Gimme More Deadpool
linkTo: deadpool-playlist
Regards,
Chanelle
Hey Adam
Both the examples above should have worked.
There must be something in your example or the data that is causing this to fail.
Here are some tricks with indexing that might help you solve the problem:
$.path.to.value
means find the value relative to the current node (not the root of the supplied context) so all paths are relative to it.
$$.path.to.value
means find the value relative the root node of the supplied context, no matter what is the current node. (internally we convert @ctx.
to $$.
when executing in expressions in jsonata).
Adding an empty array index []
in the path forces jsonata to return an array of one item vs the normal behavior where it returns the item directly.
ex: =@ctx.datasources.questions
[{}] => {}
[{},{}] => [{},{}]
ex: =@ctx.datasources.questions[]
[{}] => [{}] ← notice array of one
[{},{}] => [{},{}]
You can use the built-in index parameter from jsonata.
positional-variable-binding
This can be combined with a filter to get the correct result
ex: =@ctx.datasources.questions#$i.answer[$i=3]
JSONata exerciser
I hope this helps
Thanks all. No idea why it wasn’t working before.
I think the Jigx watcher was confusing me.
If you look at the below,
=@ctx.solution.state.question-index has a value of 3
=@ctx.datasources.questions[@ctx.solution.state.question-index].title returns a value
=@ctx.datasources.questions[3].title is empty
=$count(@ctx.datasources.questions) is empty
Thanks for sharing your experience with us Adam. We’ll take a closer look and explore how we can enhance Jigx Watcher in our future releases. {ref:86867emg2}