trim all String values in JsonNode - java

i need to trim all the String values
{
"startDate": "2015-06-29",
"endDate": "2015-07-04",
"category": "VIP ",
"name": " govind",
age: 10,
"place": " goa "
}
i am doing it by
JsonNode json = request().body().asJson();
CLassName cl = Json.fromJson(json , ClassName.class);
and trimming in setter of ClassName
suggest any other good approach because i know its not a good approach

If you can confirm that the JSON will not have quotes within values then for better performance I'd do the trimming on the raw text rather than the parsed version:
val text = request.body.asText
// RegEx explanation:
// space followed by asterisk means any number of spaces
// backward slash escapes the following character
val trimmed = text.map(_.replaceAll(" *\" *", "\""))
import play.api.libs.json.Json
val json = Json.parse(trimmed)
Java version:
import play.api.libs.json.*;
String text = request().body().asText();
String trimmed = text.replaceAll(" *\" *", "\"");
JsValue json = Json.parse(trimmed);

but changing the setter is not the right way to do it. As you would not be able to create the complete object again(even if it is required). Either you write a seperate Trimmer class which take this object and trim it. or some function in the same class. I would prefer a trimmer class

Try this:
public static void main(String[] args) {
String json =
"{"
+ " \"startDate\": \"2015-06-29\","
+ " \"endDate\": \"2015-07-04\","
+ " \"category\": \"VIP \","
+ " \"name\": \" govind\","
+ " age: 10,"
+ " \"place\": \" goa \""
+ "}";
Type stringStringMap = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map = new Gson().fromJson(json, stringStringMap);
Map<String, String> trimed = map.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey().trim(), e.getValue().trim()))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
System.out.println(trimed);
// -> {endDate=2015-07-04, name=govind, place=goa, category=VIP, age=10, startDate=2015-06-29}
}

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 should I convert the case of a JSON payload to lower camel case?

I'm dealing with JSON whose property names are snake-cased, but, I need these transformed into a lower camel case.
I don't have beans for all the corresponding objects so the usual ser-de tricks of Jackson don't apply (I think).
I tried reading it into a Map and setting a global property naming strategy but that didn't work.
String json = "{\"first_name\": \"John\", \"last_name\": \"Doe\" }";
Map<?, ?> myMap = new ObjectMapper().readValue(json, Map.class);
System.out.println(new ObjectMapper()
.setPropertyNamingStrategy(new UpperCamelCaseStrategy())
.writerWithDefaultPrettyPrinter()
.writeValueAsString(myMap));
I don't need this to be necessarily accomplished in Jackson. While a simple problem, it did leave me quite lost as to what a sane way of accomplishing this might be.
I found this solution, which is a bit hard-coded, using regex. In this way, however, you do not need to use any JsonObject library.
I wrote three methods:
one to convert a string from snake_case to pascalCase
private static String convertToPascalCase(String str){
while(str.contains("_")) {
str = str.replaceFirst("_[a-z]", String.valueOf(Character.toUpperCase(str.charAt(str.indexOf("_") + 1))));
}
return str;
}
Another which handles the replacing of the regex matches
private static String replaceMatches(Matcher m, Function<MatchResult, String> mapping) {
StringBuffer sb = new StringBuffer();
while (m.find()) {
MatchResult matchResult = m.toMatchResult();
m.appendReplacement(sb, mapping.apply(matchResult));
}
m.appendTail(sb);
return sb.toString();
}
the last one which defines the appropriate regex and then calls replaceMatches
public static String convertToPascalCaseAllJsonKeys(String jsonString) {
Pattern p = Pattern.compile("\"(\\w+)\":");
Matcher m = p.matcher(jsonString);
return replaceMatches(m, mr -> "\"" + convertToPascalCase(mr.group(1)) + "\":");
}
The usage is the following:
String json = "{\"first_name\": \"John\", \"last_name\": \"Doe\", \"children\": [\"Jane\", \"Mary\", \"Harry\"]}";
String jsonWithPascalKey = convertToPascalCaseAllJsonKeys(json);
System.out.println(jsonWithPascalKey );
// result: {"firstName": "John", "lastName": "Doe", "children": ["Jane", "Mary", "Harry"]}
Then, you can easily convert the string to a JsonObject.
https://github.com/octomix/josson
If the JSON has one level.
Josson josson = Josson.fromJsonString("{\"first_name\": \"John\", \"last_name\": \"Doe\"}");
JsonNode node = josson.getNode("entries().map(key.camelCase()::value).mergeObjects()");
System.out.println(node.toPrettyString());
Output
{
"firstName" : "John",
"lastName" : "Doe"
}
Nest the transformation query one more time for each additional level.
Josson josson = Josson.fromJsonString("{\"sales_person\":{\"first_name\": \"John\", \"last_name\": \"Doe\"}}");
JsonNode node = josson.getNode(
"entries()" +
".map(key.camelCase()" +
" ::value.entries()" +
" .map(key.camelCase()" +
" ::value)" +
" .mergeObjects())" +
".mergeObjects()");
System.out.println(node.toPrettyString());
Output
{
"salesPerson" : {
"firstName" : "John",
"lastName" : "Doe"
}
}

Convert HOCON string into Java object

One of my webservice return below Java string:
[
{
id=5d93532e77490b00013d8862,
app=null,
manufacturer=pearsonEducation,
bookUid=bookIsbn,
model=2019,
firmware=[1.0],
bookName=devotional,
accountLinking=mandatory
}
]
I have the equivalent Java object for the above string. I would like to typecast or convert the above java string into Java Object.
I couldn't type-cast it since it's a String, not an object. So, I was trying to convert the Java string to JSON string then I can write that string into Java object but no luck getting invalid character "=" exception.
Can you change the web service to return JSON?
That's not possible. They are not changing their contracts. It would be super easy if they returned JSON.
The format your web-service returns has it's own name HOCON. (You can read more about it here)
You do not need your custom parser. Do not try to reinvent the wheel.
Use an existing one instead.
Add this maven dependency to your project:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
Then parse the response as follows:
Config config = ConfigFactory.parseString(text);
String id = config.getString("id");
Long model = config.getLong("model");
There is also an option to parse the whole string into a POJO:
MyResponsePojo response = ConfigBeanFactory.create(config, MyResponsePojo.class);
Unfortunately this parser does not allow null values. So you'll need to handle exceptions of type com.typesafe.config.ConfigException.Null.
Another option is to convert the HOCON string into JSON:
String hoconString = "...";
String jsonString = ConfigFactory.parseString(hoconString)
.root()
.render(ConfigRenderOptions.concise());
Then you can use any JSON-to-POJO mapper.
Well, this is definitely not the best answer to be given here, but it is possible, at least…
Manipulate the String in small steps like this in order to get a Map<String, String> which can be processed. See this example, it's very basic:
public static void main(String[] args) {
String data = "[\r\n"
+ " {\r\n"
+ " id=5d93532e77490b00013d8862, \r\n"
+ " app=null,\r\n"
+ " manufacturer=pearsonEducation, \r\n"
+ " bookUid=bookIsbn, \r\n"
+ " model=2019,\r\n"
+ " firmware=[1.0], \r\n"
+ " bookName=devotional, \r\n"
+ " accountLinking=mandatory\r\n"
+ " }\r\n"
+ "]";
// manipulate the String in order to have
String[] splitData = data
// no leading and trailing [ ] - cut the first and last char
.substring(1, data.length() - 1)
// no linebreaks
.replace("\n", "")
// no windows linebreaks
.replace("\r", "")
// no opening curly brackets
.replace("{", "")
// and no closing curly brackets.
.replace("}", "")
// Then split it by comma
.split(",");
// create a map to store the keys and values
Map<String, String> dataMap = new HashMap<>();
// iterate the key-value pairs connected with '='
for (String s : splitData) {
// split them by the equality symbol
String[] keyVal = s.trim().split("=");
// then take the key
String key = keyVal[0];
// and the value
String val = keyVal[1];
// and store them in the map ——> could be done directly, of course
dataMap.put(key, val);
}
// print the map content
dataMap.forEach((key, value) -> System.out.println(key + " ——> " + value));
}
Please note that I just copied your example String which may have caused the line breaks and I think it is not smart to just replace() all square brackets because the value firmware seems to include those as content.
In my opinion, we split the parse process in two step.
Format the output data to JSON.
Parse text by JSON utils.
In this demo code, i choose regex as format method, and fastjson as JSON tool. you can choose jackson or gson. Furthermore, I remove the [ ], you can put it back, then parse it into array.
import com.alibaba.fastjson.JSON;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SerializedObject {
private String id;
private String app;
static Pattern compile = Pattern.compile("([a-zA-Z0-9.]+)");
public static void main(String[] args) {
String str =
" {\n" +
" id=5d93532e77490b00013d8862, \n" +
" app=null,\n" +
" manufacturer=pearsonEducation, \n" +
" bookUid=bookIsbn, \n" +
" model=2019,\n" +
" firmware=[1.0], \n" +
" bookName=devotional, \n" +
" accountLinking=mandatory\n" +
" }\n";
String s1 = str.replaceAll("=", ":");
StringBuffer sb = new StringBuffer();
Matcher matcher = compile.matcher(s1);
while (matcher.find()) {
matcher.appendReplacement(sb, "\"" + matcher.group(1) + "\"");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
SerializedObject serializedObject = JSON.parseObject(sb.toString(), SerializedObject.class);
System.out.println(serializedObject);
}
}

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

Constructing list from multi level map in Java 8

I have a multi-level map as follows:
Map<String, Map<String, Student> outerMap =
{"cls1" : {"xyz" : Student(rollNumber=1, name="test1")},
"cls2" : {"abc" : Student(rollNumber=2, name="test2")}}
Now I want to construct a list of string from the above map as follows:
["In class cls1 xyz with roll number 1",
"In class cls2 abc with roll number 2"]
I have written as follows, but this is not working, in this context I have gone through the post as well: Java 8 Streams - Nested Maps to List, but did not get much idea.
List<String> classes = outerMap.keySet();
List<String> studentList = classes.stream()
.map(cls ->
outerMap.get(cls).keySet().stream()
.map(student -> "In class "+ cls +
student + " with roll number " +
outerMap.get(cls).get(student).getRollNum() +"\n"
).collect(Collectors.toList());
You can simply use Map.forEach for this operation as:
List<String> messages = new ArrayList<>();
outerMap.forEach((cls, students) ->
students.forEach((name, student) ->
messages.add(convertToMessage(cls, name, student.getRollNumber()))));
where convertToMessage is a util as :
// this could be made cleaner using 'format'
String convertToMessage(String cls, String studentName, String rollNumber) {
return "In class" + cls + "--> " + studentName + " with roll number: " + rollNumber;
}
You may do it like so,
List<String> formattedOutput = outerMap
.entrySet().stream()
.flatMap(e -> e.getValue().entrySet().stream().map(se -> "In class " + e.getKey()
+ " " + se.getKey() + " with roll number " + se.getValue().getRollNumber()))
.collect(Collectors.toList());
You have to use the flatMap operator instead of map operator.
One method use Java 8 Stream and lambda function:
String format = "In class %s %s with roll number %d";
List<String> result = new ArrayList<>();
outerMap.entrySet().stream()
.forEach(v -> {
String className = v.getKey();
v.getValue().entrySet().stream()
.forEach(stringStudentEntry -> result.add(String.format(format,className,stringStudentEntry.getKey(),stringStudentEntry.getValue().getRollNumber())));
});

Categories