All Collections
Components
Transformer - Add values to list elements
Transformer - Add values to list elements

How to add value to different list items

Erick Rubiales avatar
Written by Erick Rubiales
Updated over a week ago

How to add a Fixed value to different list elements:

Input:

{
   "happy": "true",
  "statistics": [
    {
      "id": "A",
      "min": "2.0",
      "max": "10.0",
      "avg": "7.9"
    },
    {
      "min": "6",
      "max": "6",
      "avg": "6"
    },
    {
      "id": "C"
    }
  ]
}

Transformer Spec:

[
  {
    "operation": "modify-overwrite-beta",
// To add the value only if it does not exist, change the operation to "modify-default-beta"
    "spec": {
      "statistics": {
        "*": {
          "newValue": "test123"
        }
      }
    }
  }
]

Output:

{
  "happy": "true",
  "statistics" : [ {
    "id" : "A",
    "min" : "2.0",
    "max" : "10.0",
    "avg" : "7.9",
    "newValue" : "test123"
  }, {
    "min" : "6",
    "max" : "6",
    "avg" : "6",
    "newValue" : "test123"
  }, {
    "id" : "C",
    "newValue" : "test123"
  } ]
}

Add Dynamic Values

To dynamically add value, use, for instance, the following Transformer Spec:

[
  {
    "operation": "modify-overwrite-beta",
    // To add the value only if it does not exist, change the operation to "modify-default-beta"
    "spec": {
      "statistics": {
        "*": {
          "newValue": "@(3,happy)"
        }
      }
    }
  }
]

Remark: In the above example, number "3" indicates the number of levels that must be uploaded to access the JSON object. To better understand it, see another example below:
Input:

{
  "levelA": {
    "levelB": {
      "happy": "true"
    }
  },
  "levelX": {
    "statistics": [
      {
        "id": "A",
        "min": "2.0",
        "max": "10.0",
        "avg": "7.9"
      },
      {
        "min": "6",
        "max": "6",
        "avg": "6"
      },
      {
        "id": "C"
      }
    ]
  }
}

Transformer Spec:

[
  {
    "operation": "modify-overwrite-beta",
     "spec": {
      "nivelX": {
        "statistics": {
// the item "*" (all list elements, it is also considered a level)
          "*": {
            "newValue": "@(5,levelA.levelB.happy)"
          }
        }
      }
    }
  }
]
Did this answer your question?