Is there a way in Jolt, we can use an array of ids and extract values from a separate map to create an array of objects? Please refer this example:

huangapple go评论61阅读模式
英文:

Is there a way in Jolt, we can use an array of ids and extract values from a separate map to create an array of objects? Please refer this example:

问题

{
  "data": [
    {
      "id": "12345",
      "comment": "Test comment A",
      "key": "key A",
      "type": "CVC"
    },
    {
      "id": "67890",
      "comment": "Test comment B",
      "key": "key B",
      "type": "AVF"
    }
  ]
}
英文:

I would need to create a new array of objects based on the ids in the array of input JSON. Because the input is dynamic and can have multiple different ids.
The comment, key, and type in the textFields will have keys based on those ids.

Jolt input:

{
  "textFields": {
    "comment_12345": "Test comment A",
    "comment_67890": "Test comment B",
    "key_12345": "key A",
    "key_67890": "key B",
    "type_12345": "CVC",
    "type_67890": "AVF"
  },
  "access_id": [
    "12345",
    "67890"
  ]
}

Expected JSON output:

{
  "data": [
    {
      "id": "12345",
      "comment": "Test comment A",
      "key": "key A",
      "type": "CVC"
    },
    {
      "id": "67890",
      "comment": "Test comment B",
      "key": "key B",
      "type": "AVF"
    }
  ]
}

答案1

得分: 1

我们可以循环遍历 access_id 值,然后将每个值用作 id 并从 textFields 中获取其他值,使用 access_id 值。

所以您可以使用这个规范:

[
  {
    "operation": "shift",
    "spec": {
      "access_*": {
        "*": {
          "*": {
            "@1": "data[&2].&(3,1)",
            "@(4,textFields.comment_&)": "data[&2].comment",
            "@(4,textFields.key_&)": "data[&2].key",
            "@(4,textFields.type_&)": "data[&2].type"
          }
        }
      }
    }
  }
]
英文:

We can loop through access_id values and use each value as id and get other values from textFields with the access_id values.

So you can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "access_*": {
        "*": {
          "*": {
            "@1": "data[&2].&(3,1)",
            "@(4,textFields.comment_&)": "data[&2].comment",
            "@(4,textFields.key_&)": "data[&2].key",
            "@(4,textFields.type_&)": "data[&2].type"
          }
        }
      }
    }
  }
]

答案2

得分: 1

然而,您可以通过以下方法使您的情况更加动态,即分别循环textFields对象和access_id数组,通过在对象键中的下划线后面的值与数组的组件之间的共同因素分隔,使用以下方法作为转换规范:

[
  {
    "operation": "shift",
    "spec": {
      "textFields": {
        "*_*": "&(0,2).&(0,1)" // "下划线后的部分"."下划线前的部分"
      },
      "access_*": {
        "*": {
          "@": "@(3,&2[&1]).&(2,1)" // "通过遍历一个`:`和两个`{`向上走3层树;同时使用数组的索引"."向上走2层并抓取星号的第一个替代"
        }
      }
    }
  },
  { // 摒弃改革后的对象键
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

在网站http://jolt-demo.appspot.com/上的演示如下图所示:

Is there a way in Jolt, we can use an array of ids and extract values from a separate map to create an array of objects? Please refer this example:

英文:

Yet, you can make your case more dynamic as looping both textFields object
and access_id array independently through separating by common factors which
come from the values following underscores within the keys of the object versus
components of the array using the method below as the transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "textFields": {
        "*_*": "&(0,2).&(0,1)" // "the piece after underscore"."the piece before underscore"
      },
      "access_*": {
        "*": {
          "@": "@(3,&2[&1]).&(2,1)" // "going 3 levels up the tree by traversing one `:` and two `{`; while using the indexes of the array"."going 2 levels up and grab the 1st replacement of asterisk"   
        }
      }
    }
  },
  { // get rid of the reformed object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

Is there a way in Jolt, we can use an array of ids and extract values from a separate map to create an array of objects? Please refer this example:

huangapple
  • 本文由 发表于 2023年3月4日 06:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定