英文:
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/上的演示如下:
<details>
<summary>英文:</summary>
The trick is to qualify the values(on the RHS) by `@1,id` notation such as
```json
[
{
"operation": "shift",
"spec": {
"userList": {
"*": { // renaming the attributes occur in the next two lines
"id": "@1,id.userId",
"address|accessFullName": "@1,id.address",
"subComponentId": "@1,id.&"
}
},
"*": { // other stuff than the "userList" array
"detail": "@1,id.inputUI",
"textValue": "@1,id.displayDetail"
}
}
},
{ // get rid of the object keys
"operation": "shift",
"spec": {
"*": {
"@": ""
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论