edit-json
In this section, you will find the reference for a Declarative Hook of the
edit-jsontype.
A Declarative Hook of the edit-json type defines changes made to existing JSON files. See the following example of a Declarative Hook definition of the edit-json type:
name: "hook-edit-json"
description: Test edit-json in hooks
types:
- app
spec:
inputs:
- label: Just a text
type: text
name: json_body
- label: Just a Tag
type: text
name: json_tag
hooks:
- type: edit-json
trigger: after-render
path: package.json
indent: "\t"
encoding: utf-8
changes:
- jsonpath: "$.scripts"
update:
value: |
{
"test": "ola 123",
"ola": "como vai você"
}
- jsonpath: "$"
remove:
- name: private
- name: devDependencies
- jsonpath: "$.log"
clear: true
Available Actions
path:
Defines the path of the file that will be edited. The path can be composed of Jinja expressions.
path: package.json
trigger:
Field to set triggers that define when file editing should occur.
before-input:
Executes the Declarative Hook before receiving the input parameters from the user.
trigger: before-input
before-render:
Executes the Declarative Hook before the Template generates files in the project.
trigger: before-render
after-render:
Executes the Declarative Hook after the Template generates files in the project.
trigger: after-render
encoding:
Sets the encoding type to be written to the file and accepts the formats ascii, utf-8, and utf-16. The default value is utf-8.
encoding: utf-8
indent:
Defines the character(s) or number of spaces for the indentation of the generated document. The default value for spaces is 2.
indent: "\t"
changes:
It is a list of editing actions that are executed in the order they appear. The block has jsonpath as a required field.
During the search process in the changes attribute, all nodes that match the criteria (the jsonpath attribute) are changed at once. You can perform multiple changes in a file by using expressions.
Check out some of the supported expressions:
Click here to check the expressions!
| Expression | Description |
|---|---|
@ | Defines the object of the current element. |
$ | Defines the object of the root element. |
() | Script expression using the underlying script engine. |
[start:end] | Returns a copy of part of an array created between the start and end positions (end not included) of an original array. |
?() | Applies the filter to the script expression. |
[] | Child operator used in expressions. |
.. | Recursive descent. |
* | Used on object elements regardless of whether you use their names. |
[] | In JSON and JavaScript, it is the native array operator. |
[,] | The JSONPath allows a set of indexes and array names. |
jsonpath:
Allows you to define the JSON search expression. Its use is mandatory.
changes:
- jsonpath: "$.packages.version"
...
If necessary, check the JSONPath support.
You can interpolate the values of the jsonpath field with Jinja:
hooks:
- type: edit-json
trigger: after-render
path: package.json
indent: "\t"
encoding: utf-8
changes:
- jsonpath: "$.scripts[?name = '{{INTERPOLAR-INPUT-AQUI}}']"
update:
value: |
{
"test": "ola 123",
"ola": "como vai você"
}
update:
Changes or adds an item within a jsonpath result. Available fields are value or snippet.
value
Receives the value to be added to JSON.
update:
value: "qa"
snippet
Receives the snippet to be added to JSON.
update:
snippet: snippets/fragment.json
remove:
Removes a dictionary element. The available field is name.
name:
Gets the name of the element to be removed from the JSON dictionary. You can interpolate the values of the name field with Jinja.
- jsonpath: "$.scripts"
remove:
- name: prepare
clear:
Removes all content from the JSON node, given the jsonpath context. Accepted values are true or false.
- jsonpath: "$.devDependencies"
clear: true
when:
Conditional action that evaluates whether the insertion will take place. The available option is not-exists, which receives the text or file to be evaluated by the condition.
not-exists:
Checks whether the values entered in the value field exist in the file. If they exist, the update, remove, and clear actions will not be performed.
hooks:
- type: edit-json
jsonpath: "$.some"
update:
value: |
{
"not-existing": {
"item": 2
}
}
when:
not-exists: "$.some.not-existing"