Building New Array from Items

Hello again n8n community!

I’m trying to transform data output from an Airtable Node to build an array to send to the Telegram API and build a custom inline keyboard.

My Airtable Node is outputting data which looks like this:

[
  {
    "id": "rec1VJOChv1nkxfN4",
    "createdTime": "2021-04-05T16:04:14.000Z",
    "fields": {
      "Asset ID": "RCE-99-0003",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  },
  {
    "id": "recKrCqheDiw29HgL",
    "createdTime": "2021-04-05T19:04:15.000Z",
    "fields": {
      "Asset ID": "RCE-99-0006",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  },
  {
    "id": "recRZ730mzyVgl3k2",
    "createdTime": "2021-04-05T19:04:14.000Z",
    "fields": {
      "Asset ID": "RCE-99-0005",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  },
  {
    "id": "recdqVya9y7IEWnsJ",
    "createdTime": "2021-04-05T19:04:13.000Z",
    "fields": {
      "Asset ID": "RCE-99-0004",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  },
  {
    "id": "rece2k7GKAvQfRbiH",
    "createdTime": "2021-04-05T16:04:14.000Z",
    "fields": {
      "Asset ID": "RCE-99-0002",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  },
  {
    "id": "reclwjjZuGNCmCLsq",
    "createdTime": "2021-04-05T16:04:14.000Z",
    "fields": {
      "Asset ID": "RCE-99-0001",
      "Item Name String": "Motorola - EVX-261-G7-5",
      "Status": "Available"
    }
  }
]

I need to transform this data to look like this:

{
	"reply_markup": {
		"inline_keyboard": [
			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "rec1VJOChv1nkxfN4"
            }
			],
			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "recKrCqheDiw29HgL"
            }
			],
			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "recRZ730mzyVgl3k2"
            }
			],
			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "recdqVya9y7IEWnsJ"
            }
			],
			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "rece2k7GKAvQfRbiH"
            }
			],
          			[{
					"text": "Motorola - EVX-261-G7-5 (Available)",
					"callback_data": "reclwjjZuGNCmCLsq"
                    }
			]
		]
	}
}

Values should be mapped to the new array like so:

{
	"reply_markup": {
		"inline_keyboard": [
			[{
					"text": "{fields.['Item Name String']}+' ('+{fields.Status}+')'",
					"callback_data": "{id}"
            }
			]
		]
	}
}

I’ve spent hours trying to do this myself using some combination of the Item Lists node and jsmepath, but can’t for the life of me figure it out and I would so incredibly grateful if there is someone here that can point me in the right direction!

Hey @Felix_is_stuck,

Give this a go, It should get you closer.

1 Like

Thank you @Jon!!

1 Like

I wanted to add here that after more tinkering I did manage to get JMESpath to function as I needed and will share that expression here in case of interest to others. @Jon’s code snippet is more flexible for my use case though and so I’ve updated my workflow to use that approach.

{{$jmespath($json.data, "[*].[{ text:fields.buttonText, callback_data: join('',[fields.Status,id,':'])}]" )}}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.