Free Support Forum - aspose.com

Filtering on Json and applying functions on the elements in template syntax

Hi Aspose team,

How can you filter in the template syntax on arrays in the json source?

example json:

> {
> 	"items": [{
> 		"item1": {
> 			"id": "1",
> 			"key1": "value1",
> 			"key2": "value2"
> 		},
> 		"item2": {
> 			"id": "2",
> 			"key1": "value1",
> 			"key2": "value2"
> 		},
> 		"item3": {
> 			"id": "3",
> 			"key1": "value1",
> 			"key2": "value2"
> 		}
> 	}]
> }

I’d like to reach the id of an element in the items[] that cointains(‘3’) or startWith(“item3”).

I tried something as:

<<foreach[item in items]>><<if[item.contains('3')]>>Id = <<[item.id]>><<else>> no item with 3 in the array <</if>><</foreach>>

However, this does not compile. How can you make use of functions to check the element with an condition? or is there any other way to get to the same result?

Many thanks,
Jeffrey

@Jeffrey1,

We are checking this scenario and will get back to you soon.

@Jeffrey1,

First of all, in the JSON sample you shared, the “items” array contains just a single item. This item is an object having complex properties (that is, properties themselves are objects): “item1”, “item2”, and “item3”. Object properties cannot be treated in the same way as array items. If you want to treat them as array items, then you should turn them into array items as shown in the following JSON snippet:

{
     "items": [
     {
           "name": "item1",
           "id": "1",
           "key1": "value1",
           "key2": "value2"
     },
     {
           "name": "item2",
           "id": "2",
           "key1": "value1",
           "key2": "value2"
     },
     {
           "name": "item3",
           "id": "3",
           "key1": "value1",
           "key2": "value2"
     }
     ]
 }

Then the following template should work fine:

<<foreach[item in items]>><<if[item.name.contains("3")]>>Id = <<[item.id]>><<else>> no item with 3 in the array <</if>><</foreach>>