Mathieu De Keyzer's face

 How to extract data from a JsonMenuNestedEditorField

HTTP enthusiasts

Goal

When handling data in a elasticms JsonMenuNestedEditorField we should be able to parse the structure to get the content for the website search.

Computed field

The idea is to parse the Json menu, at finalization, in an computed field.

Regardless the type of container, let assume that I should extract those data fields:

  • label
  • body
  • value
  • sub-title

Here is the value template.

{% set data = {} %}

{% for item in _source.blocks|default('{}')|ems_json_menu_nested_decode %}
     {% if item.label is defined %}
         {% set data = data|merge([item.label]) %}
     {% endif %}
     {% if item.object.body is defined %}
         {% set data = data|merge([item.object.body|striptags]) %}
     {% endif %}
     {% if item.object.value is defined %}
         {% set data = data|merge([item.object.value]) %}
     {% endif %}
     {% if attribute(item.object, 'sub-title') is defined %}
         {% set data = data|merge([attribute(item.object, 'sub-title')]) %}
     {% endif %}
{% endfor %}

{{ data|json_encode|raw }}

To check what has been extracted define this Display template.

{{ data|json_encode }}

And finally the mapping to define:

{
    "type" : "text",
    "copy_to": "live_search"
}