← All recipes
Open source

Add value to JSON Object

Recipe IDorg.openrewrite.json.AddKeyValue
Artifactorg.openrewrite:rewrite-json

Adds a value at the specified keyPath with the specified key, if the key doesn't already exist.

Single recipeApache 2.0

Usage

You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.json.AddKeyValue --recipe-option "keyPath='$.subjects.*' or '$' or '$.x[1].y.*' etc." --recipe-option "key=myKey" --recipe-option "value=`"myValue"` or `{"a": 1}` or `[ 123 ]`"

If the recipe isn’t available locally, install it with:

mod config recipes jar install org.openrewrite:rewrite-json:8.88.0

Options

NameTypeDescription
keyPathrequiredStringA JsonPath expression to locate the *parent* JSON entry.
e.g. '$.subjects.*' or '$' or '$.x[1].y.*' etc.
keyrequiredStringThe key to create.
e.g. myKey
valuerequiredStringThe value to add to the document at the specified key. Can be of any type representing JSON value. String values should be quoted to be inserted as Strings.
e.g. `"myValue"` or `{"a": 1}` or `[ 123 ]`
prependBooleanIf set to true the value will be added to the beginning of the object

Data tables

Structured output this recipe can produce.

  • Source files that had resultsSource files that were modified by the recipe run.org.openrewrite.table.SourcesFileResults
  • Source files that had search resultsSearch results that were found during the recipe run.org.openrewrite.table.SearchResults
  • Source files that errored on a recipeThe details of all errors produced by a recipe run.org.openrewrite.table.SourcesFileErrors
  • Recipe performanceStatistics used in analyzing the performance of recipes.org.openrewrite.table.RecipeRunStats