jsonjslt

How can I implement a mapping table in JSLT?


That is, for each possible value in a specific field in the incoming JSON I want to map to some new value. But those values are not predictable, so I need a mapping table. How can I express this in JSLT?


Solution

  • The easiest way to do it is to write the mapping table as a JSON object, and then use the get-key function. So something like this:

    let mapping = {
      "foo" : 1,
      "bar" : 2,
      // ...
      "baz" : 24
    }
    
    { "new-field" : get-key($mapping, .field) }
    

    For the input {"field" : "bar"} you'll output {"new-field" : 2}.