Search code examples
lookupjsonata

How to use JSONata $lookup() inside an array?


I'm trying to use an auxiliary mapping table to translate the property content. "Phone" is the data and "aux" is the object with the mapping table.

{
  "Phone": [
    {
      "type": "home",
      "number": "0203 544 1234"
    },
    {
      "type": "office",
      "number": "01962 001234"
    },
    {
      "type": "office",
      "number": "01962 001235"
    },
    {
      "type": "mobile",
      "number": "077 7700 1234"
    }
  ],
  "aux" : {
    "home" : "casa",
    "office" : "escritório",
    "mobile" : "celular"
  }
}

$lookup seems a good fit and I wrote the following JSONata code

{
    "Telefone" : Phone.{
        "tipo" : $lookup(aux, type),
        "numero" : number 
    },
    "tipo" : $lookup(aux, Phone[0].type)
}

"tipo" inside the array is not working, but outside, for any specific array member seems to work fine.

{
  "Telefone": [
    {
      "numero": "0203 544 1234"
    },
    {
      "numero": "01962 001234"
    },
    {
      "numero": "01962 001235"
    },
    {
      "numero": "077 7700 1234"
    }
  ],
  "tipo": "casa"
}

What am I missing? Thanks


Solution

  • I just found the problem. I should use the root to refer the mapping table, such as

    "tipo" : $lookup($$.aux, type),