Roman Svystun
Roman Svystun

Reputation: 135

JOLT json array on a match apply String default

Having the following input json:

{
  "familyMembers": [
    {
      "name": "Richard",
      "living": "Yes"
    },
    {
      "name": "Napoleon",
      "living": "No"
    }
  ]
}

the goal is to change "living": "Yes" to "living": "true", so that desired output would be like this:

{
  "familyMembers" : [
    {
      "firstName" : "Richard",
      "living": "true"
    },
    {
      "firstName" : "Napoleon",
      "living": "false"
    }
  ]
}

The following spec does not produce a desired output:

[
  {
    "operation": "shift",
    "spec": {
      "familyMembers": {
        "*": {
          "name": "familyMembers[&1].firstName",
          "living": {
            "Yes": {
              "#true": "familyMembers[&1].living"
            },
            "*": {
              "#false": "familyMembers[&1].living"
            }
          }
        }
      }
    }
  }
]

The living field is not included. If the familyMember is changed from an array to a map, then the living field is included in the output with the expected value like in this example: http://jolt-demo.appspot.com/#hashDefault

I've been struggling with this case for couple of days now with various combinations of &, [], # and digits. No luck so far.

A spec like this

[{
  "operation": "shift",
  "spec": {
    "familyMembers": {
      "*": {
        "name": "familyMembers[&1].firstName",
        "living": {
          "Yes": {
            "#true": "familyMembers[#].living"
          },
          "*": {
            "#false": "familyMembers[#].living"
          }
        }
      }
    }
  }
}]

produces

{
  "familyMembers" : [
    {
      "firstName" : "Richard",
      "living" : [
        "true",
        "false"
      ]
    },
    {
      "firstName" : "Napoleon"
    }
  ]
}

which is not what is required.

Any hints or help are welcome.

Upvotes: 1

Views: 451

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

Start by symbolizing the other case by * than living as in your conditional logic nested within the object with key name living as Yes vs. *. Then, convert the key names familyMembers and living by appropriate & substitution as counting the { signs upto the level to reach their conforming names along with bracketed substitutions([&1]and[&3]) which should reach the conforming index symbol under familyMembers array.

For example you'll count twice { in order to reach living key from its counterpart, and replace the leaf livings with &2 such as

[
  {
    "operation": "shift",
    "spec": {
      "familyMembers": {
        "*": {
          "living": {
            "Yes": {
              "#true": "&4[&3].&2"
            },
            "*": {
              "#false": "&4[&3].&2"
            }
          },
          "*": "&2[&1].&"
        }
      }
    }
  }
]

enter image description here

Upvotes: 1

Related Questions