最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Jolt spec to lookup keys of an array - Stack Overflow

programmeradmin0浏览0评论

I am trying to replace values of an array based on a lookup map using Jolt, but the spec does not seem to work. It works till reaching the lookup map and I can shift the entire map, but referring to each key using & is not working as expected.

Input:

{
  "records": [
    {
      "id": "1",
      "keys": [
        "ba2c89c3",
        "aa2c89c3"
      ]
    }
  ],
  "lookup": {
    "ba2c89c3": "EE",
    "aa2c89c3": "EL"
  }
}

Expected Output:

{
  "data": {
    "records": [
      {
        "id": "1",
        "vals": [
          "EE",
          "EL"
        ]
      }
    ]
  }
}

Spec I tried but doesn't seem to work.

[
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "id": "data.records[&1].id",
          "keys": {
            "*": {
              "@(4,lookup.&)": "data.records[&3].vals"
            }
          }
        }
      }
    }
  }
]

I am trying to replace values of an array based on a lookup map using Jolt, but the spec does not seem to work. It works till reaching the lookup map and I can shift the entire map, but referring to each key using & is not working as expected.

Input:

{
  "records": [
    {
      "id": "1",
      "keys": [
        "ba2c89c3",
        "aa2c89c3"
      ]
    }
  ],
  "lookup": {
    "ba2c89c3": "EE",
    "aa2c89c3": "EL"
  }
}

Expected Output:

{
  "data": {
    "records": [
      {
        "id": "1",
        "vals": [
          "EE",
          "EL"
        ]
      }
    ]
  }
}

Spec I tried but doesn't seem to work.

[
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "id": "data.records[&1].id",
          "keys": {
            "*": {
              "@(4,lookup.&)": "data.records[&3].vals"
            }
          }
        }
      }
    }
  }
]

Share Improve this question edited Feb 3 at 17:58 Barbaros Özhan 65.4k11 gold badges36 silver badges61 bronze badges asked Feb 3 at 12:04 Jobin ThomasJobin Thomas 1451 silver badge12 bronze badges 1
  • The use of & in "@(4,lookup.&)" is using the index of the array instead of the value as we are iterating through the keys array. How to use the value here instead of the index? – Jobin Thomas Commented Feb 3 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 0

You can match by the following shift transformation spec in which the innermost "&" on the left hand side will do the trick :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "id": "data.&2[&1].&",//&2[&1] represents "records" array along with subindexes  
          "keys": {
            "*": {
              "*": {
                "@5,lookup": { //need to go 5 levels up to be able to the whole nesting
                  "&": "data.&6[&5].vals[]"//& on the left side provides the equality between the values  
                }
              }
            }
          }
        }
      }
    }
  }
]

where vals array will completely disappear if none of the values match

the demo on the site Jolt Transform Demo Using v0.1.1 is :

The accepted answer does indeed solve the transformation problem, but posting this solution which I ended up using.

As the issue was with using & to refer to value while iterating over the keys array which was instead providing the index, what worked is to to get the value using * as provided in the accepted solution.

[
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "id": "data.records[&1].id",
          "keys": {
            "*": {
              "*": {
                "@(5,lookup.&)": "data.records[&4].vals"
              }
            }
          }
        }
      }
    }
  }
]
发布评论

评论列表(0)

  1. 暂无评论