← All recipes
Open source

Remove empty keys

Recipe IDorg.openrewrite.json.RemoveEmptyKeysArtifactorg.openrewrite:rewrite-json

Remove mapping entries whose value is an empty object or array, such as those left behind by DeleteKey. Entries are removed from the inside out, so a chain of objects holding nothing but the removed entry is removed entirely. Array elements are left alone, since removing one shifts the indexes of its siblings.

Single recipeApache 2.0

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.json.RemoveEmptyKeys

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

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

Options

NameTypeDescription
keysStringA JsonPath expression bounding the cleanup. Keys it selects, and the keys nested within them, are eligible for removal. When omitted, the whole document is eligible.
e.g. $..devDependencies
cascadeToStringA JsonPath expression letting removal continue outward past keys. A key this recipe empties is removed when cascadeTo selects it or nests it, so keys of $.spec.template with cascadeTo of $.spec goes on to remove spec once template is gone. Keys that were already empty outside keys are still left alone. When omitted, removal stops at the keys boundary.
e.g. $.spec

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