JMESPath - 连接嵌套数组中的项目

时间:2018-05-30 01:03:45

标签: jmespath

我有一个JSON

{
"key": "processId-29231",
"fields": {
    "attachment": [
        {
            "id": "79572",
            "filename": "File1.png"
        },
        {
            "id": "74620",
            "filename": "File2.docx"
        },
        {
            "id": "79072",
            "filename": "File3.xlsx"
        }
    ]
  }
}

我需要将其重组为此

{
"processId": "processId-29231",
"attachments": [

               "https://example.com/files/79572/File1.png",
               "https://example.com/files/79572/File2.docx",
               "https://example.com/files/79572/File1.xlsx",
                ]
    }

我可以使用特定的数组索引

{processID:key,attachments:join('',['https://example.com/files/',fields.attachment[1].id,'/',fields.attachment[1].filename])}

产生这个结果

{
 "processID": "processId-29231",
 "attachments": "https://example.com/files/74620/File2.docx"
}

我在没有数组索引的情况下尝试了两种方式

这个

 {processID:key,attachments:join('',['https://example.com/',fields.attachment[].id,'/',fields.attachment[].filename])}

和这个

{processID:key,attachments:join('', ['https://example.com/',fields.attachment[*].id,'/',fields.attachment[*].filename])}

但这没有帮助。

有关如何解决此问题的任何提示?

1 个答案:

答案 0 :(得分:1)

您需要将函数应用于数组的每个元素,并通过@

访问当前节点
{processID:key,attachments: fields.attachment[].join('',['https://example.com/files/', @.id, '/', @.filename])}

http://jmespath.org/specification.html#functions