This is really out of scope for JSON libraries, since JSON format itself has no support or notion of variable substitution. Your best bet may be to use a JSON library (like Jackson) to get a tree representation (for Jackson that would be JsonNode), then traverse it, and use another library for handling textual substitution. There are many that can do that, from stringtemplate to others (perhaps MessageFormat that other answer refers to).

It may also be possible to revert the other, if your substitutions will never funny "funny characters" (quotes, linefeeds); if so, you could use string templating lib first, JSON parser next feeding processed text. But it is bit riskier, as usually there is eventually one case where you do end up trying to add a quote, say, and then parsing fails.

Answer from StaxMan on Stack Overflow
Discussions

How to replace a value of a json file in java - Stack Overflow
I need some help with replacing a line in a json file. I have attempted using the same method i use to replace a line in a text file with no success. Basically the json file contains the following ... More on stackoverflow.com
🌐 stackoverflow.com
How do I replace JSON variables in a JSON file with native JSON VIs.
I am now doing it by opening the JSON file using the file IO VIs, reading the full file to a string and then using string manipulations like match regular expression an search and replace string. I then replace the whole file with the manipulated string. This is now working, but I thought it ... More on forums.ni.com
🌐 forums.ni.com
1
0
November 25, 2021
java - Is there a general string substitution function similar to sl4fj? - Stack Overflow
If you are looking for a solution where you can replace a bunch of variables in a String with values, you can use StrSubstitutor. More on stackoverflow.com
🌐 stackoverflow.com
java - How to including variables within strings? - Stack Overflow
So, we all should know that you can include variables into strings by doing: String string = "A string " + aVariable; Is there a way to do it like: String string = "A string {aVaria... More on stackoverflow.com
🌐 stackoverflow.com
🌐
How to do in Java
howtodoinjava.com › home › string › java text block formatting with variables/expressions
Java Text Block Formatting with Variables/Expressions
January 10, 2024 - This is the most cleaner and recommended way of formatting text blocks with variables. It returns a formatted string using the specified format string (text block) and arguments (variables). Consider the following example. Here we are formatting a JSON string with variables in runtime:
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2023 › 07 › 19 › __trashed
Replace Value in JSON Using JsonPath in Java | Jayway JsonPath | Rest Assured |
[ { "full name": "Everett Fritsch", "address": { "street": "898 Chesley View", "city": "Zing" }, "skills": [ "Maths", "Science" ] }, { "full name": "Stacey Witting", "address": { "street": "993 Keebler Summit", "city": "La Unión" }, "skills": [ "Java", "Python" ] } ] ... We will change the “full name” of the first element of the JSON array. ... We will replace all the “full name” in the array. @Test public void changeValueOfKeyUsingJsonPath() { // Replace value and converting new JSON to string jsonObject = JsonPath.parse(jsonObject).set("$[0]['full name']", "Amod Mahajan").jsonString
🌐
Medium
medium.com › @papapapaya11 › exploring-various-ways-to-handle-jsonobject-in-java-6edef1f0561c
Exploring Various Ways to Handle JSONObject in Java | by Little Miss Brave | Medium
December 2, 2024 - String jsonTemplate = "{\"greeting\": \"Hello, ${name}!\", \"age\": ${age}}"; Map<String, String> values = new HashMap<>(); values.put("name", "Little Miss Brave"); values.put("age", "26"); StringSubstitutor substitutor = new StringSubstitutor(values); String result = substitutor.replace(jsonTemplate); JSONObject jsonObject = JSONObject.fromObject(result); System.out.println(jsonObject); // {"greeting": "Hello, Little Miss Brave!", "age": 26} If there are other special use cases, they will be continuously updated here. By mastering these methods, you’ll be well-equipped to handle JSON data efficiently in Java applications.
🌐
GitHub
gist.github.com › amr › 897109c2c9a5d3b317e6a97fe74f321f
Java simple YAML/JSON/Properties configuration mechanism with environment variable substitution/interpolation · GitHub
The thing is, after trying other dominent libraries, I didn't find anything that gave me the functionality that I got from Jackson + StringSubstitutor in 20 lines of code. Map configurations to POJOs, with all Jackson mapping goodness · Supports variable substitution, from environment and you can tweak to add more sources · Supports YAML, JSON and all the other formats Jackson supports
Find elsewhere
🌐
Apache Commons
commons.apache.org › proper › commons-text › apidocs › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.15.0 API)
Gets the StringLookup that is used to lookup variables. ... Gets the variable default value delimiter matcher currently in use. ... Gets the variable prefix matcher currently in use. ... Gets the variable suffix matcher currently in use. ... Returns a flag whether substitution is disabled in variable values.If set to true, the values of variables can contain other variables will not be processed and substituted original variable is evaluated, e.g.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-2.6 › org › apache › commons › lang › text › StrSubstitutor.html
StrSubstitutor (Commons Lang 2.6 API) - Apache Commons
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is not deleted. ... Internal method that substitutes the variables.
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › Processor-for-replacing-JSON-values-dynamically-and › m-p › 106274
Solved: Re: Processor for replacing JSON values dynamicall... - Cloudera Community - 106272
May 15, 2018 - Thanks Matt for the quick response. I am already using ReplaceText with now{}, however I tried AttributesToJSON but somehow the attribute is not getting updated in the output JSON. Below is what I am trying along with the NiFi screenshot, kindly advice what I might be missing here - Input JSON - {"e...
🌐
Octopus Deploy
octopus.com › docs › projects › steps › configuration-features › structured-configuration-variables-feature
Structured Configuration Variables | Documentation and Support
January 1, 2023 - The Target File field also supports Variable Substitution Syntax, to allow things like referencing environment-specific files, or conditionally including them based on scoped variables. Extended template syntax allows conditionals and loops to be used. Structured Configuration Variables allows for replacement in JSON, YAML, XML, and Properties files.
🌐
GitHub
github.com › strongloop › loopback-next › issues › 2556
Variable substitution in DataSource JSON files · Issue #2556 · loopbackio/loopback-next
July 3, 2019 - By using ENV. prefix, we are keeping doors open to add other variable sources in the future, e.g. APP. prefix to access application-level bindings. To support numeric values, allow the following format (notice the + sign after the opening bracket): ... A new helper function accepting configuration object loaded from the JSON file, an object with values to use for substitutions (conceptually {ENV: process.env}), and returning modified configuration object with substitutions resolved according to rules described above.
Author   bajtos
🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - String interpolation is a straightforward and precise way to inject variable values into a string. It allows users to embed variable references directly in processed string literals.