0

I have a JSON payload saved as a String

String jsonBody = “{\n”
            + ” \“example\“: {\n”
            + ”  \“example\“: [\n”
            + ”   {\n”
            + ”    \“example\“: 100,\n”
            + ”    \“this_is_example_json_key\“: \“this_is_example_json_value\“,\n”

I created that by copying body from i.e Postman into

String jsonBody = "here I pasted the body";

Unfortunately I cannot have everything hardcoded there, so I have to change some values to variables. The JSON in postman looks like:

"this_is_example_json_key":"x"

And so on. Let's assume that:

String x = “this_is_example_json_value“;

If I just replace it like

+ ” \“this_is_example_json_key\“: \“ + x + \“,\n”

or something like that, the value in the body will be just this_is_example_json_value, where I need "this_is_example_json_value" (the "" marks are part of the value).

So the question is, how to set up those + / " in the String, so in the end in the value of the JSON I will end up with the value inside " ".

I've tried to play with the " / + but nothing of those were working. Variable must be passed with those " " because otherwise, the API is sending back an error.

1
  • It's unclear what JSON you want to end up with. Please add that to your question. Commented Dec 7, 2022 at 3:20

3 Answers 3

2

Since java 15, if you want only use the string, you can also do in this way:

int this_is_example_json_value= 100;
String json = """
{
    "this_is_example_json_key":  %d
}
""".formatted(this_is_example_json_value);

Here the official jep.

Sign up to request clarification or add additional context in comments.

Comments

2

Don't try to build up JSON using strings. Use a proper JSON parser.

import org.json.JSONException;
import org.json.JSONObject;

public class Eg {
    public static void main(String[] args) throws JSONException {
        String x = "this_is_example_json_value";
        JSONObject example = new JSONObject();
        example.put("this_is_example_json_key", x);
        System.out.println(example.toString());
    }
}

Which outputs:

{"this_is_example_json_key":"this_is_example_json_value"}

With no messing around wondering what needs to be escaped.

1 Comment

I will keep that in mind when I will be able to, but in this case I have there, there is just no point of doing that. But really appreciate your help, thank you!
1

you can use an extra " \ " "

    String x = "this_is_example_json_value";

    String jsonBody = "{\n"
            + "\"example\": {\n"
            + "  \"example\": [\n"
            + "  {\n"
            + " \"example\": 100,\n"
            + "\"this_is_example_json_key\":" + "\"" + x + "\"" + "\n }"
            +"\n  ]\n   }\n    }";

in this case you will get a json string

{
  "example": {
    "example": [
      {
        "example": 100,
        "this_is_example_json_key": "this_is_example_json_value"
      }
    ]
  }
}

2 Comments

@tgdavies I am sorry , but the json string is shown already, why it should be repeated?
Sorry I meant to put that comment on the Question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.