在Jolt中,如何在对象数组中使用字段值来引用外部键?

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

In Jolt,, how do we use a field value inside an array of objects to refer to an outside key?

问题

以下是翻译好的内容:

我需要执行位操作并循环遍历 'userList' 来为新的结构分配 'address'、'metadata' 等内容。在这个过程中,我需要引用每个对象的 'id' 并从数组外部的 "USER_DETAILS_id" 对象中提取用户详细信息。

在循环遍历数组时,我无法使用 'id' 的值来从 "USER_DETAILS_id" 中提取用户详细信息。我尝试在 'id' 中使用 "&",但这不起作用。

我的输入:

{
    "otherList": [
        {
            "id": "def",
            "displayDetail": "Text here."
        },
        {
            "id": "abc",
            "displayDetail": "Text"
        }
    ],
    "userList": [
        {
            "id": "FIRST-d87c-11ed-a612",
            "address": "123 Elm street",
            "metadata": {
                "userInput": "",
                "userOutput": ""
            },
            "subComponentId": "12.1"
        },
        {
            "id": "SECOND-d87c-11ed-a612",
            "accessFullName": "456 Little Elm street",
            "metadata": {
                "userInput": "",
                "userOutput": ""
            },
            "subComponentId": "12.2"
        }
    ],
    "USER_DETAILS_FIRST-d87c-11ed-a612": {
        "id": "FIRST-d87c-11ed-a612",
        "textValue": "Text value for first user.",
        "detail": "Comp|Boorgu,Johny|04-11-23"
    },
    "USER_DETAILS_SECOND-d87c-11ed-a612": {
        "id": "SECOND-d87c-11ed-a612",
        "textValue": "Text value for second user.",
        "detail": "Limit|Kaur,Rajesh|04-11-23"
    }
}

所以我的输出可能是类似这样的:

{
    "components": [
        {
            "componentId": "1",
            "isEditable": true,
            "type": "other",
            "subComponent": [
                {
                    "subComponentId": "1.1",
                    "otherId": "def",
                    "inputUI": "",
                    "displayDetail": "Text here."
                },
                {
                    "subComponentId": "1.2",
                    "otherId": "abc",
                    "inputUI": "",
                    "displayDetail": "Text"
                }
            ]
        },
        {
            "componentId": "2",
            "isEditable": true,
            "type": "user",
            "subComponent": [
                {
                    "subComponentId": "2.1",
                    "userId": "FIRST-d87c-11ed-a612",
                    "address": "123 Elm street",
                    "inputUI": "Comp|Boorgu,Johny|04-11-23",
                    "displayDetail": "Text value for second user."
                },
                {
                    "subComponentId": "2.2",
                    "userId": "SECOND-d87c-11ed-a612",
                    "address": "456 Little Elm street",
                    "inputUI": "Limit|Kaur,Rajesh|04-11-23",
                    "displayDetail": "Text value for first user."
                }
            ]
        }
    ]
}
英文:

I would need to perform shift operation and loop through 'userList' to assign 'address', 'metadata', etc to form a new structure. While doing so, I need to refer to 'id' of each objects and extract the user details from the object "USER_DETAILS_id" that is outside the array.

I am not able to use the value of 'id' while looping through the array to extract the user details from "USER_DETAILS_id". I tried using "&" inside "id" but that would not work.

My input:

{
    "otherList": [
        {
            "id": "def",
            "displayDetail": "Text here."
        },
        {
            "id": "abc",
            "displayDetail": "Text"
        }
    ],
    "userList": [
        {
            "id": "FIRST-d87c-11ed-a612",
            "address": "123 Elm street",
            "metadata": {
                "userInput": "",
                "userOutput": ""
            },
            "subComponentId": "12.1"
        },
        {
            "id": "SECOND-d87c-11ed-a612",
            "accessFullName": "456 Little Elm street",
            "metadata": {
                "userInput": "",
                "userOutput": ""
            },
            "subComponentId": "12.2"
        }
    ],
    "USER_DETAILS_FIRST-d87c-11ed-a612": {
        "id": "FIRST-d87c-11ed-a612",
        "textValue": "Text value for first user.",
        "detail": "Comp|Boorgu,Johny|04-11-23"
    },
    "USER_DETAILS_SECOND-d87c-11ed-a612": {
        "id": "SECOND-d87c-11ed-a612",
        "textValue": "Text value for second user.",
        "detail": "Limit|Kaur,Rajesh|04-11-23"
    }
}

So my output could be something like:

{
    "components": [
        {
            "componentId": "1",
            "isEditable": true,
            "type": "other",
            "subComponent": [
                {
                    "subComponentId": "1.1",
                    "otherId": "def",
                    "inputUI": "",
                    "displayDetail": "Text here."
                },
                {
                    "subComponentId": "1.2",
                    "otherId": "abc",
                    "inputUI": "",
                    "displayDetail": "Text"
                }
            ]
        },
        {
            "componentId": "2",
            "isEditable": true,
            "type": "user",
            "subComponent": [
                {
                    "subComponentId": "2.1",
                    "userId": "FIRST-d87c-11ed-a612",
                    "address": "123 Elm street",
                    "inputUI": "Comp|Boorgu,Johny|04-11-23",
                    "displayDetail": "Text value for second user."
                },
                {
                    "subComponentId": "2.2",
                    "userId": "SECOND-d87c-11ed-a612",
                    "address": "456 Little Elm street",
                    "inputUI": "Limit|Kaur,Rajesh|04-11-23",
                    "displayDetail": "Text value for first user."
                }
            ]
        }
    ]
}

答案1

得分: 1

[
  {
    "operation": "shift",
    "spec": {
      "userList": {
        "*": {
          "id": "@1,id.userId",
          "address|accessFullName": "@1,id.address",
          "subComponentId": "@1,id.&"
        }
      },
      "*": {
        "detail": "@1,id.inputUI",
        "textValue": "@1,id.displayDetail"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": ""
      }
    }
  }
]

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

在Jolt中,如何在对象数组中使用字段值来引用外部键?


<details>
<summary>英文:</summary>
The trick is to qualify the values(on the RHS) by `@1,id` notation such as
```json
[
{
&quot;operation&quot;: &quot;shift&quot;,
&quot;spec&quot;: {
&quot;userList&quot;: {
&quot;*&quot;: { // renaming the attributes occur in the next two lines
&quot;id&quot;: &quot;@1,id.userId&quot;,
&quot;address|accessFullName&quot;: &quot;@1,id.address&quot;,
&quot;subComponentId&quot;: &quot;@1,id.&amp;&quot;
}
},
&quot;*&quot;: { // other stuff than the &quot;userList&quot; array
&quot;detail&quot;: &quot;@1,id.inputUI&quot;,
&quot;textValue&quot;: &quot;@1,id.displayDetail&quot;
}
}
},
{ // get rid of the object keys
&quot;operation&quot;: &quot;shift&quot;,
&quot;spec&quot;: {
&quot;*&quot;: {
&quot;@&quot;: &quot;&quot;
}
}
}
]

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

在Jolt中,如何在对象数组中使用字段值来引用外部键?

huangapple
  • 本文由 发表于 2023年6月8日 01:54:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425912.html
匿名

发表评论

匿名网友

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

确定