Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMESPath - Joining items in a nested array

Tags:

jmespath

I have a JSON

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

I need to restructure it to this

{
"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",
                ]
    }

I could make this work with a specific array index

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

which yields this result

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

I tried this without array index in two ways

this

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

and this

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

but that did not help.

Any tips on how this can be solved?

like image 965
user9445 Avatar asked May 30 '18 01:05

user9445


1 Answers

You need to apply the function to each element of the array and access the current node via @

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

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

like image 123
diedu Avatar answered Sep 28 '22 01:09

diedu