Skip to content

JSON Usage

Micci - Luiz Miccieli edited this page Jan 30, 2025 · 5 revisions

Overview

The JSON handler allows you to define and replace specific JSON keys or values with placeholders configured through the Templify plugin. This setup is beneficial for projects where JSON files, such as environment configurations, require consistent updates or adaptations.

JSON Configuration Structure

Below is an example configuration file for handling JSON files:

steps:
  - kind: JsonHandler
    apiVersion: v1
    spec:
      - files:
          - env.json
        placeholders:
          - match: $['name']
            replace: templify.name

Explanation of Key Sections

1. kind: JsonHandler

This defines the handler type as JsonHandler, specifying that the operations pertain to JSON files.

2. apiVersion: v1

Indicates the version of the API for handling JSON configurations. Ensure compatibility by matching the correct API version.

3. spec Section

Each spec item contains:

  • files: Specifies the JSON files in which replacements will occur. The paths are relative to the root of the project.
  • placeholders: Defines the placeholder operations within each file, where:
    • match: Represents the Json path expression targeting the element or text to be replaced.
    • replace: Specifies the placeholder value that will substitute the matched content. This value can reference an item in the templify configuration, providing flexibility to customize the output.

Example Use Cases

env.json Replacements

The env.json file has a placeholder configured to automate specific values:

  • $['name']: Replaces the name field value with the value specified in templify.name.

Example project

You can see this example working here