Can this module read JSON or XML files?

Created on 23 February 2024, about 1 year ago
Updated 24 February 2024, about 1 year ago

Hello,

Can this module read JSON or XML files, and load them into Twig for use?

Thank you.

💬 Support request
Status

Closed: duplicate

Version

3.3

Component

Miscellaneous

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @msn5158
  • 🇷🇺Russia Chi

    Check out source Twig function.

  • Although I don't know how to use source Twig function to load JSON files, because I tried, No effect seen.
    I still hope that this module can add this feature, such as through drupal_loads_json() or other names.

    Thanks

  • 🇷🇺Russia Chi

    drupal_loads_json()

    That would just duplicate source function from Twig core.

  • /sites/default/files/json/test.json
    Json Data:
    {
    "nid": 1,
    "title": "Sample One",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing."
    }

    Use by Twig:
    {% set data = source('/sites/default/files/json/test.json', ignore_missing = true) %}

    {% for key, value in data %}
    {{ key }}: {{ value }}
    {% endfor %}

    No output, Is there anything wrong with it?

  • Status changed to Closed: duplicate about 1 year ago
  • 🇷🇺Russia Chi

    source() just loads file content. It does not decode it. You need something like json_decode.

    See related issue for explanations.

    For such case I would recommend loading data in preprocess hook. That should be trivial.

    function example_preprocess(array $variables): void {
      $json = \file_get_contents('public://test.json');
      $variables['test'] = \json_decode($json);
    }
    
Production build 0.71.5 2024