Skip to main content

Getting values from a hash using a path string

  • February 25, 2026
  • 0 replies
  • 17 views

Theo Hollenberg
Forum|alt.badge.img+3

Anyone else who builds functional blocks in for instance webhooks?

As I’m creating a lot of webhooks that I use as functional blocks, I tend to pass around a lot of JSON. One of the implications is that want to retrieve values from these JSON hashes using variable paths.

This little function has helped me a lot there:

<#function getValueFromPath source={} path="">
<#assign parts = path?split(".")>
<#assign current = source>
<#list parts as p>
<#if p?matches(".*\\[\\d+\\]")>
<#assign key = p?replace("\\[\\d+\\]", "", "r")>
<#assign idx = p?replace(".*\\[(\\d+)\\]", "$1", "r")?number>
<#assign current = current[key][idx]!>
<#else>
<#assign current = current[p]!>
</#if>
</#list>
<#return current>
</#function>

There’s a multitude of ways to use this.

One method I use a lot is to send an incoming body to my ‘Update Asset’ webhook which holds a body like below, where I pass the asset id, the fields to update and the paths where to find the values, and the source so search in which gives me a small reusable piece of functionality.

The status value I use for setting my status field afterwards to help other processes to identify if a step in a specific workflow has been tackles successfully.

{
"assetId": "a83f3e97-5db2-42be-a989-8e5a5c8ff2be",
"status": "ST-03-02",
"paths": {
"width": "dimensions.width",
"height": "dimensions.height",
"firstRating": "reviews[0].rating"
},
"source": {
"id": 1,
"title": "Essence Mascara Lash Princess",
"description": "The Essence Mascara Lash Princess is a popular mascara known for its volumizing and lengthening effects. Achieve dramatic lashes with this long-lasting and cruelty-free formula.",
"category": "beauty",
"price": 9.99,
"discountPercentage": 7.17,
"rating": 4.94,
"stock": 5,
"tags": [
"beauty",
"mascara"
],
"brand": "Essence",
"sku": "RCH45Q1A",
"weight": 2,
"dimensions": {
"width": 23.17,
"height": 14.43,
"depth": 28.01
},
"warrantyInformation": "1 month warranty",
"shippingInformation": "Ships in 1 month",
"availabilityStatus": "Low Stock",
"reviews": [
{
"rating": 2,
"comment": "Very unhappy with my purchase!",
"date": "2024-05-23T08:56:21.618Z",
"reviewerName": "John Doe",
"reviewerEmail": "john.doe@x.dummyjson.com"
},
{
"rating": 2,
"comment": "Not as described!",
"date": "2024-05-23T08:56:21.618Z",
"reviewerName": "Nolan Gonzalez",
"reviewerEmail": "nolan.gonzalez@x.dummyjson.com"
},
{
"rating": 5,
"comment": "Very satisfied!",
"date": "2024-05-23T08:56:21.618Z",
"reviewerName": "Scarlett Wright",
"reviewerEmail": "scarlett.wright@x.dummyjson.com"
}
],
"returnPolicy": "30 days return policy",
"minimumOrderQuantity": 24,
"meta": {
"createdAt": "2024-05-23T08:56:21.618Z",
"updatedAt": "2024-05-23T08:56:21.618Z",
"barcode": "9164035109868",
"qrCode": "..."
},
"thumbnail": "...",
"images": [
"...",
"...",
"..."
]
}
}