Liquid Template nested expression with special characters in the path - java

Let's say I have the following map:
{
context: {
'parent-name': { /* data */ },
'parent-name/http': { /* data */ },
}
self: {
id: 'parent-name'
}
}
Now I want to reference the parent-name/http but itself as a variable. I wrote the following expression:
{{context[self.id/http]}}
Using the https://github.com/bkiers/Liqp Java SDK, this throws an error:
parser error "mismatched input '/' expecting {'.', NEq, '==', '>=',
'>', '<=', '<', '[', ']', '?', 'contains', 'and', 'or'}" on line 1,
index 24
How do I write such expression?

AFAIK, you can't do that in one go. You'll first have to create the key, by using | append: "/http", and then retrieve the value with this key:
String json = "{\n" +
" \"context\": {\n" +
" \"parent-name\": \"mu\",\n" +
" \"parent-name/http\": \"foo\"\n" +
" }," +
" \"self\": {\n" +
" \"id\": \"parent-name\"\n" +
" }\n" +
"}";
Template t = Template.parse(
"{% assign key = self.id | append: \"/http\" %}" +
"{{context[key]}}");
System.out.println(t.render(json));

Related

Inserting String variable into JSON which is in a form of String

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.
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.
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.
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"
}
]
}
}

How to fill in empty double value in test DTO, Java, Spring

So I am testing the controller of my application and I stumbled upon a problem.
This is the Json I use in my testcode:
String bike = "{" +
" \"bikeNumber\" : \"E2\"," +
" \"brand\" : \"Gazelle\"," +
" \"frameNumber\" : \"HA1234568\"," +
" \"retailPrice\" : 1200," +
" \"basePrice\" : 20.0," +
" \"electric\" : true" +
"}";
I want to test when the user doesn't fill in one of the values, it throws an exception. In other words I want to test the #NotNull, #NotBlank annotations.
I have done this for bikeNumber and frameNumber, these are string types and I can leave them open. The problem I get if I leave the basePrice open and run my test, I get an error saying:
java.lang.AssertionError: No value at JSON path "$.basePrice"
I know that it's a null value because I haven't filled it in, but that is part of the plan. So my question is, how do I leave a long, double or int value empty without getting a test error.
My full code block is here:
#Test
void whenPostRequestNoBasePrice_thenBadRequestResponse() throws Exception {
String bike = "{" +
" \"bikeNumber\" : \"E1\"," +
" \"brand\" : \"Gazelle\"," +
" \"frameNumber\" : \"HA1234568\"," +
" \"retailPrice\" : 1200," +
" \"basePrice\" : \"\" " +
" \"electric\" : true" +
"}";
mockMvc.perform(MockMvcRequestBuilders.post("/createbike")
.content(bike)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.basePrice", Is.is( "Base price is mandatory.")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse().getContentAsString();
}
I tried multiple things, also with and without the comma: "basePrice" : "" ," +
Also I can't seem to find this anywhere on stackoverflow.

Reading element one by one in JsonArray

I have below JSON returned by API call.
String jsonString="{\r\n" +
" \"count\": 100,\r\n" +
" \"limit\": 100,\r\n" +
" \"totalResults\": 225,\r\n" +
" \"hasMore\": true,\r\n" +
" \"items\": [\r\n" +
" {\r\n" +
" \"id\": \"55D40522-8672-48B0-B225-FD3CE6686AD6\",\r\n" +
" \"name\": \"AcmeHCMExtended\",\r\n" +
" \"version\": \"20.01.01.03\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": \"2DB4C83B-0CF9-4A8E-AC41-A29B30324121\",\r\n" +
" \"name\": \"AFinancialBot\",\r\n" +
" \"version\": \"1.0\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": \"7EA85B81-3CA1-4BE0-B095-217E7C93DCEF\",\r\n" +
" \"name\": \"AIAMFG\",\r\n" +
" \"version\": \"1.0\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" }"
I want to read name one by one under the itmes. I am using below code.
JsonObject obj=Json.parse(jsonString).asObject();
JsonArray items=obj.get("items").asArray();
System.out.println(items); //it prints everything under items.
`System.out.println(items.name); ` //It gives error
I am not able to iterate item array. it does not show length property of array.
Can you guide me how can I access element one by one.
It looks like you are using the javax.json.JsonArray APIs or something similar. If so:
JsonArray items = obj.get("items").asArray();
for (int i = 0; i < items.size(); i++) {
System.out.println(items.getJsonObject(i));
}
Note that JsonArray implements java.util.List and you are getting the size using the standard List API method.
The same code would work with com.google.gson.JsonArray ... though this class does NOT implement List. Alternatively, you could make use of the fact that com.google.gson.JsonArray implements Iterable and write a "for each" loop:
for (JsonElement e: items) {
System.out.println(e);
}
The equivalent code for org.json.JSONArray uses length() instead of size().
In all cases, you should be able to answer your own question by searching for and reading the javadocs for the JSON library that you are using.
UPDATE - Apparently you are using the com.eclipsesource.minimal-json. According to this javadoc, it has a size() method and implements Iterable.

JSON string could not convert to array/ArrayList

[
{
countryCode: "CN",
countryCallingCode: "+86",
codeRule: "^1\d{10}$"
},
{
countryCode: "US",
countryCallingCode: "+1",
codeRule: "^\d{10}$"
}
]
So I define the model like this in Kotlin
data class CountryCallingCode(
val countryCode: String,
val countryCallingCode: String,
val codeRule: String? = null
)
This is what the backend document defines the response.
codeRule is regex expression to verify a phone number.
I stuck in convert the String to List.
I pasted them to Android Studio, and it shows like:
String response = "[\n" +
" {\n" +
" countryCode: \"CN\",\n" +
" countryCallingCode: \"+86\",\n" +
//" codeRule: \"^1\\d{10}$\"\n" +
" },\n" +
" {\n" +
" countryCode: \"US\",\n" +
" countryCallingCode: \"+1\",\n" +
//" codeRule: \"^\\d{10}$\"\n" +
" }\n" +
"]";
The following codes do not work.
Converting code 1:
Gson gson = new Gson()
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);
And I think the following codes are the same, correct me if I am wrong.
Converting code 2:
ArrayList<CountryCallingCode> countryCallingCodeList = (ArrayList<CountryCallingCode>)gson.fromJson(response, ArrayList.class);
Converting Code 3
Gson gson = new Gson();
Type type = new TypeToken<List<CountryCallingCode>>() {
}.getType();
List<CountryCallingCode> countryCallingCodeList = gson.fromJson(response, type);
Then I use https://jsoneditoronline.org/ to reformat my json.
I tried to remove the codeRule, and pasted to the Android Studio, it also tells me I am wrong, it shows CountryCode causes SyntaxException.
String reponse = "[\n" +
" {\n" +
" countryCode: \"CN\",\n" +
" countryCallingCode: \"+86\"\n" +
" },\n" +
" {\n" +
" countryCode: \"US\",\n" +
" countryCallingCode: \"+1\"\n" +
" }\n" +
"]";
Only I compress the jsonstring to oneline, I could convert the jsonstring to the array/ArrayList.
String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\"}]";
Does anyone know
Q1:
How to deal with codeRule?
Q2:
Why couldn't I pasted the origin JsonString to the Android Studio?
Why I must compress the JsonString into one line string?
Updated:
Origin one line json string:
[{"countryCode":"CN","countryCallingCode":"+86", codeRule: "^1\d{10}$"},{"countryCode":"US","countryCallingCode":"+1", codeRule: "^\d{10}$"}]
Pasted result json string:
String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\", codeRule: \"^1\\d{10}$\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\", codeRule: \"^\\d{10}$\"}]";
Code:
Gson gson = new Gson();
/* Convertion 1 */
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);
Error:
result = {JsonSyntaxException#7237} Method threw 'com.google.gson.JsonSyntaxException' exception.
cause = {MalformedJsonException#7241} "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
detailMessage = "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
stackState = null
stackTrace = {StackTraceElement[28]#7243}
suppressedExceptions = {Collections$EmptyList#7244} size = 0
shadow$_klass_ = {Class#1694} "class com.google.gson.JsonSyntaxException"
shadow$_monitor_ = -2082115852
This is more of a comment than an answer, but I need more space to explain - if your version of Java supports it, can you try creating your response with a raw string like this:
String response = `[
{
countryCode: "CN",
countryCallingCode: "+86",
codeRule: "^1\d{10}$"
},
{
countryCode: "US",
countryCallingCode: "+1",
codeRule: "^\d{10}$"
}
]`
Or if possible use Kotlin, which uses """ to delimit raw strings. It will be much more readable and might help you spot mistakes

How to pass dynamic value to a JSON String

I have constructed a JSON String this way , but cannot able to pass dynamic values to it
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"String\",\r\n" +
" \"ActualMessage\": \"String\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
I need to pass dynamic value to the ActaulMessage atribute
Please tell me how ?
i have tried number of trial and errors but couldn't able to succeed.
Use string concatenation.
String message = "this is value want to pass to the ActualMessage attribute " ;
String input = "{\r\n" +
"\"Level\": 0,\r\n" +
"\"Name\": \"String\",\r\n" +
"\"msgName\": \"String\",\r\n" +
"\"ActualMessage\": \"" + message + "\",\r\n" +
"\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
How about using String.format() for this? for example, to pass a "dynamic value" declare a place holder in the text:
String input = "insert %s in the string"; // here %s is the placeholder
input = String.format(input, "value"); // replace %s with actual value
Now input will contain the string "insert value in the string". In your example, change this line:
" \"msgName\": \"String\",\r\n"
Replace it with this:
" \"msgName\": \"%s\",\r\n"
Now you can perform the substitution:
input = String.format(input, message);
Notice that the first parameter in the format() method has a lot more of options, and that you can pass more than one argument to be replaced. Take a look at the documentation for the Formatter class.
if you want to manipulate Json please consider GSON. your problem can be addressed as follows.
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"MessageName\",\r\n" +
" \"ActualMessage\": \"%s\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
String output=String.format(input,message);
//this will replace %s with the content of message variable.

Categories