SMS2: ID for JSON Lists

I am converting the following JSON structure succesfully via a SMS2 mapper:

...

FROM JSON{

 {
  [
     {"utterance": "?utt"}, 
  ]
 }

}TO {
?hit has :Utterance.
?utterance a :Utterance .
       :has_text ?utt .
...
}

Example input and output:

{
  [
     {"utterance": "Hello "}, 
     {"utterance": "world"}, 
  ]
 }

<:Message123> :has <:Utterance1> ;
                           :has <:Utterance2> .
<:Utterance1> :has_text "Hello" .
<:Utterance2> :has_text "World" .

Is there a way to keep track of the order of the array using SMS2? The result should be something like:

<:Message123> :has <:Utterance1> ;
                           :has <:Utterance2> .
<:Utterance1> :has_text "Hello" ;
                        :has_id 1 .
<:Utterance2> :has_text "World" ;
                        :has_id 2 .

So how could I add an iterator to:

?hit has :Utterance.
?utterance a :Utterance .
       :has_text ?utt ; 
       :has_id <????my_iterator_solution????>

...

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

There is currently no facility to preserve order of elements in an array. You could do a transformation using the jq utility as follows. I've added a key to correspond to your array:

{
    "values":[
     {"utterance": "Hello "},
     {"utterance": "world"}
  ]
}

You can transform it like so:

$ cat utterances.json | jq '.values | to_entries | map({value:.value, index:.key})'
[                                                                                                                                      
  {                                                                                                                                    
    "value": {                                                                                                                         
      "utterance": "Hello "                                                                                                            
    },                                                                                                                                 
    "index": 0                                                                                                                         
  },                                                                                                                                   
  {                                                                                                                                    
    "value": {                                                                                                                         
      "utterance": "world"                                                                                                             
    },                                                                                                                                 
    "index": 1                                                                                                                         
  }                                                                                                                                    
]

Jess

1 Like