yfaney
yfaney

Reputation: 83

Convert a value in the json array into another using JOLT

I have this input

[
  {
    "date": "2020-01-01",
    "state_cd": "CA"
  },
  {
    "date": "2020-01-03",
    "state_cd": "NY"
  },
  {
    "date": "2020-01-05",
    "state_cd": "WA"
  }
]

and would like to convert state_cd to 1 if it's CA or 0 otherwise.

I tried the following spec,

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "state_cd": {
          "CA": {
            "#1": "[].ca_ind"
          },
          "*": {
            "#0": "[].ca_ind"
          }
        },
        "*": "[&1].&0"
      }
    }
  }
]

but the state cd is one index skewed from what I supposed to like this.

[
  {
    "date" : "2020-01-01"
  },
  {
    "ca_ind" : "1",
    "date" : "2020-01-03"
  },
  {
    "ca_ind" : "0",
    "date" : "2020-01-05"
  },
  {
    "ca_ind" : "0"
  }
]

Could anyone help me how to address this issue?

Upvotes: 1

Views: 98

Answers (2)

yfaney
yfaney

Reputation: 83

K, Jagadesh gave me the idea what my actual spec should look like:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "state_cd": {
          "CA": {
            "#1": "[&3].ca_ind"
          },
          "*": {
            "#0": "[&3].ca_ind"
          }
        },
        "*": "[&1].&"
      }
    }
  }
]

Thanks!

Upvotes: 0

Jagadesh
Jagadesh

Reputation: 2116

Check this spec,

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "date": "[&1].date",
        "state_cd": {
          "CA": {
            "#1": "[&3].ca_ind"
          },
          "*": {
            "#0": "[&3].ca_ind"
          }
        }
      }
    }
  }
]

Upvotes: 1

Related Questions