# How to dynamically set a value based on another key-value

**URL:** <https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617>\
**Category:** Data Transformers\
**Tags:** data-transformer\
**Created:** [May 31, 2023, 8:28am UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617 "2023-05-31T08:28:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjetil](https://forum.alumio.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kjetil](https://forum.alumio.com/u/kjetil)\
**Post date:** [May 31, 2023, 8:28am UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617/1 "2023-05-31T08:28:30Z")

</div>

In my JSON object I have one key-value, _condition_, from which I want to decide which other object to set in a third value. This condition can vary in different datasets/objects. How can that be done using JmesPath or some copy-move-transformer? So far I have only seen examples with hardcoded control values.

example:

```json
{
    "condition":"good",
    "states":{
        "bad":{
            "emotion":"sad",
            "color":"red"
        },
        "good":{
            "emotion":"happy",
            "color":"green"
        },
        "neutral":{
            "emotion":"meh",
            "color":"yellow"
        }
    }
}

```

`resultState` = `states` that have the key of `condition`

```json
{
  "resultState": {
    "emotion":"happy",
    "color":"green"
  }
}
```

---

<div class="post-metadata">

**Author:** ![kjetil](https://forum.alumio.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kjetil](https://forum.alumio.com/u/kjetil)\
**Post date:** [May 31, 2023, 10:01am UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617/2 "2023-05-31T10:01:46Z")

</div>

I found a solution based on [this former post](https://forum.alumio.com/t/copy-data-between-each-other-by-keys/517/3). By rewriting the example object to an array containing the key-value for comparison.

```json
{
  "options": [
    {
      "color": "red",
      "emotion": "sad",
      "condition2": "bad"
    },
    {
      "color": "green",
      "emotion": "happy",
      "condition2": "good"
    },
    {
      "color": "yellow",
      "emotion": "meh",
      "condition2": "neutral"
    }
  ],
  "condition1": "good"
}

```

Then Recursively copy the outer condition into each of the array objects and finally make a value setter to copy the matching object using jmespath:  
myCondition.string = `&{options[?condition2 == condition1]}`

Still it would be interesting to know if this can be done directly without the recursive copying.

---

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [May 31, 2023, 12:24pm UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617/3 "2023-05-31T12:24:45Z")

</div>

Hi @kjetil

We are glad that you managed to get it worked by having a look at another topic in this forum.

As far as I know, unfortunately, this is the only shortest way to achieve your objective. The JMESPath filter expression (`array[?x == y]`) can only filter where `x` and `y` are properties within each array object or a static value like `'123'` or `'test'`.

---

<div class="post-metadata">

**Author:** ![abubakar](https://forum.alumio.com/letter_avatar_proxy/v4/letter/a/8491ac/32.png) [@abubakar](https://forum.alumio.com/u/abubakar)\
**Post date:** [June 8, 2023, 3:31pm UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617/4 "2023-06-08T15:31:37Z")

</div>

Hello everyone, I have attempted to compare the `barCode` value with the `_bar` value within the `meta` section. However, regardless of whether the values are the same or different, I consistently receive the same response.

{  
“barCode”: “876545689”,  
“meta”: [  
{  
“\_bar”: “8765456890”  
},  
{  
“\_street”: “hjguu667”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![kjetil](https://forum.alumio.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kjetil](https://forum.alumio.com/u/kjetil)\
**Post date:** [June 8, 2023, 6:20pm UTC](https://forum.alumio.com/t/how-to-dynamically-set-a-value-based-on-another-key-value/617/5 "2023-06-08T18:20:03Z")

</div>

Remember to first copy the barcode value into the children.  
When the barcode is not the same you will get an empty array. When it’s equal, you will get that array object.

You can check this example:  
[export\_20230608211654.ndjson](https://forum.alumio.com/uploads/short-url/k9GBAXaqz8ujn7aUm8ShdNhTaPb.ndjson) (1.4 KB)
