Ran Nissan
Ran Nissan

Reputation: 132

How to access json fields with Jolt Transform?

How do I access json fields with Jolt transform?

For example I have this json:

{
  "a": 110,
  "b": 10
}

I would like to have:

{
  "a": 110,
  "b": 10,
  "c": 100   // 110 - 10 (substraction)
}

Upvotes: 0

Views: 724

Answers (2)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

You just can use a single modify-overwrite-beta transformation along with a intSubtract function in order to add add an extra element to the current json value such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "c": "=intSubtract(@(1,a),@(1,b))"
    }
  }
]

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881653

The following transformation will add a c variable which is set to a - b:

[
  {
    "operation": "shift",
    "spec": {
      "a": "a",
      "b": "b"
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "c": "=intSubtract(@(1,a), @(1,b))"
    }
  }
]

If you wish to test it, the Jolt demo website is an excellent resource. Put your original JSON into the "JSON Input" box:

{
  "a": 110,
  "b": 10
}

Then place the transformation spec from the top of this answer into the "JOLT Spec" box and hit the Transform button. The result should be as you desired:

{
  "a" : 110,
  "b" : 10,
  "c" : 100
}

Upvotes: 2

Related Questions