We can surely, define string in Strings.xml & call - in that case the line break is not an issue.
But suppose I want to put it in my java page -- if I put it as follows - it will bring error
String strJson="
{
\"Employee\" :[
{
\"id\":\"01\",
\"name\":\"Gopal Varma\",
\"salary\":\"500000\"
},
{
\"id\":\"02\",
\"name\":\"Sairamkrishna\",
\"salary\":\"500000\"
},
{
\"id\":\"03\",
\"name\":\"Sathish kallakuri\",
\"salary\":\"600000\"
}
]
}";
I can fix the error by making it in a single line
String strJson=" { \"Employee\" :[ { \"id\":\"01\",\"name\":\"Gopal Varma\",\"salary\":\"500000\"},{\"id\":\"02\",\"name\":\"Sairamkrishna\",\"salary\":\"500000\"}, { \"id\":\"03\", \"name\":\"Sathish kallakuri\", \"salary\":\"600000\" } ] }";
But I want to know, instead is there any escape char or something to fix the error.
Java String literals cannot span multiple lines, but you can do this.
String strJson = "{\n" +
" \"Employee\" :[\n" +
" {\n" +
" \"id\":\"01\",\n" +
" \"name\":\"Gopal Varma\",\n" +
" \"salary\":\"500000\"\n" +
" }\n" +
" ]\n" +
"}";
You need to escape the strings properly,
Try this,
String strJson = "{" +
"\"Employee\": [{" +
"\"id\": \"01\"," +
"\"name\": \"Gopal Varma\"," +
"\"salary\": \"500000\"" +
"}, {" +
"\"id\": \"02\"," +
"\"name\": \"Sairamkrishna\"," +
"\"salary\": \"500000\"" +
"}, {" +
"\"id\": \"03\"," +
"\"name\": \"Sathish kallakuri\"," +
"\"salary\": \"600000\"" +
"}]" +
"}";
Related
I'm getting this deeply nested JSON response from an api that I have no control,
What should be the best way to get to "generalDetails" and then find the first true value under security, address, account and mobile?
{
"info_code": "201",
"info_description": "info description",
"data": {
"status": "here goes the status",
"failure_data": {
"source": "anySource",
"details": {
"data": {
"server_response": {
"generalDetails": {
"security": {
"isAccountLocked": "false"
},
"address": {
"isAddresExists": "true"
},
"account": {
"accountExists": "true",
"isValidAccount": "true"
},
"mobile": {
"mobileExists": "true"
}
}
}
}
}
}
}
}
My request looks like:
#Autowired
private WebClient.Builder webClientBuilder;
String resp = webClientBuilder.build().get().uri(URL)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class).block();
First, build the model, automatic here https://codebeautify.org/json-to-java-converter.
Then read data with the model
.bodyToMono(MyData.class)
Then decide how you want evaluate the requirement find the first true value under security, address, account and mobile.
What means "first" ? JSON has no natural order without indicating explicity (e.g. field "order": 2).
N.B. "true", "false" of the response are Strings, not booleans.
Once you have the model with data, you may do:
Object firstTrue(GeneralDetails gd) {
// No null checks here
if ("true".equals(gd.getSecurtity().isLockedAccount())) return gd.getSecurtity();
if ("true".equals(gd.getAddress().isAddressExists())) return gd.getAddress();
if ("true".equals(gd.getAccount().isAccountExists()) || "true".equals(gd.getAccount().isAccountValid())) return gd.getAccount();
if ("true".equals(gd.getMobile().isMobileExists())) return gd.getMobile();
return null;
}
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"info_code\": \"201\"," +
" \"info_description\": \"info description\"," +
" \"data\": {" +
" \"status\": \"here goes the status\"," +
" \"failure_data\": {" +
" \"source\": \"anySource\"," +
" \"details\": {" +
" \"data\": {" +
" \"server_response\": {" +
" \"generalDetails\": {" +
" \"security\": {" +
" \"isAccountLocked\": \"false\"" +
" }," +
" \"address\": {" +
" \"isAddresExists\": \"true\"" +
" }," +
" \"account\": {" +
" \"accountExists\": \"true\"," +
" \"isValidAccount\": \"true\"" +
" }," +
" \"mobile\": {" +
" \"mobileExists\": \"true\"" +
" }" +
" }" +
" }" +
" }" +
" }" +
" }" +
" }" +
"}");
Query
JsonNode node = josson.getNode(
"data.failure_data.details.data.server_response" +
".generalDetails.**.mergeObjects().assort().[*]");
System.out.println(node.toPrettyString());
Output
{
"isAddresExists" : "true"
}
If changed isAddresExists and accountExists to false
" \"generalDetails\": {" +
" \"security\": {" +
" \"isAccountLocked\": \"false\"" +
" }," +
" \"address\": {" +
" \"isAddresExists\": \"false\"" +
" }," +
" \"account\": {" +
" \"accountExists\": \"false\"," +
" \"isValidAccount\": \"true\"" +
" }," +
" \"mobile\": {" +
" \"mobileExists\": \"true\"" +
" }" +
" }" +
Output
{
"isValidAccount" : "true"
}
If you only want the key name
String firstTureKey = josson.getString(
"data.failure_data.details.data.server_response" +
".generalDetails.**.mergeObjects().assort().[*].keys().[0]");
System.out.println(firstTureKey);
Output
isValidAccount
I have a custom #Query in one of my elasticsearch repositories because the autoGenerated method didn't use match (instead query_string with analyze_wildcard) and so didn't work for example with spaces. This query looks pretty simple to me so I thought it wouldn't be a problem to write it myself.
#Query("\"bool\": { " +
" \"filter\": [ " +
" { " +
" \"term\": { " +
" \"userId.keyword\": \"?0\" " +
" } " +
" }, " +
" {" +
" \"match\": { " +
" \"content\": \"?1\" " +
" }" +
" } " +
" ] " +
" }")
Page<SearchablePageHistory> findAllByUserIdAndContentLike(String userId, String content, Pageable pageable);
But when I try to execute that function I get the following error:
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=x_content_parse_exception, reason=Failed to derive xcontent]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1793) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1770) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1527) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1484) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1454) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:970) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.lambda$search$10(ElasticsearchRestTemplate.java:265) ~[spring-data-elasticsearch-4.0.0.RELEASE.jar:4.0.0.RELEASE]
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:351) ~[spring-data-elasticsearch-4.0.0.RELEASE.jar:4.0.0.RELEASE]
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.search(ElasticsearchRestTemplate.java:265) ~[spring-data-elasticsearch-4.0.0.RELEASE.jar:4.0.0.RELEASE]
at org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery.execute(ElasticsearchStringQuery.java:89) ~[spring-data-elasticsearch-4.0.0.RELEASE.jar:4.0.0.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/history/_search?pre_filter_shard_size=128&typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=dfs_query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"x_content_parse_exception","reason":"Failed to derive xcontent"}],"type":"x_content_parse_exception","reason":"Failed to derive xcontent"},"status":400}
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:283) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:261) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1514) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
... 124 common frames omitted
With debuggin I tracked down the raw Rest-Request that is sent to elasticsearch in org.elasticsearch.client.RestClient.java:244 and found that this is the payload sent to the server:
{"from":0,"size":10,"query":{"wrapper":{"query":"ImJvb2wiOiB7ICAgICJmaWx0ZXIiOiBbICAgICB7ICAgICAgICJ0ZXJtIjogeyAgICAgICAidXNlcklkLmtleXdvcmQiOiAiMzFjMjA5NTktNjg5Zi00YjI4LWExNzctNmQ3ZTQ2YTBhYzMwIiAgICAgIH0gICAgIH0sICAgeyJtYXRjaCI6IHsgICAgImNvbnRlbnQiOiAidGVzdHNzIiAgIH19ICAgIF0gICB9"}},"version":true,"sort":[{"id":{"order":"desc"}}]}
With that payload an error is not suprising however I have no idea why there is this weird mumble of characters. I suspect that that is supposed to be my custom query which is not used correctly. I got this payload by debugging into this line:
httpResponse = client.execute(context.requestProducer, context.asyncResponseConsumer, context.context, null).get();
and then executing:
StandardCharsets.UTF_8.decode(((NByteArrayEntity) ((HttpPost) ((HttpAsyncMethods.RequestProducerImpl) context.requestProducer).request).entity).buf).toString()
These are my imports and the classname that I use in the Repository-Class:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.stream.Stream;
public interface SearchablePageHistoryRepository extends ElasticsearchRepository<SearchablePageHistory, Integer> {
Page<SearchablePageHistory> findAllByUserId(String userId, Pageable pageable);
#Query("\"bool\": { " +
" \"filter\": [ " +
" { " +
" \"term\": { " +
" \"userId.keyword\": \"?0\" " +
" } " +
" }, " +
" {" +
" \"match\": { " +
" \"content\": \"?1\" " +
" }" +
" } " +
" ] " +
" }")
Page<SearchablePageHistory> findAllByUserIdAndContentLike(String userId, String content, Pageable pageable);
}
All other queries where I don't use #Query work fine without a problem. I have no idea what I am doing wrong since my example seems very similar to the one given in the documentation: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.query-methods.at-query
Hard facepalm, I found my error -> still gonna leave this post up in case someone else stumbles across the same problem since the error message is not very helpful in my opinion.
I simply forgot the surrounding brackets around the outside of the query:
changing this:
#Query("\"bool\": { " +
" \"filter\": [ " +
" { " +
" \"term\": { " +
" \"userId.keyword\": \"?0\" " +
" } " +
" }, " +
" {" +
" \"match\": { " +
" \"content\": \"?1\" " +
" }" +
" } " +
" ] " +
" }")
Page<SearchablePageHistory> findAllByUserIdAndContentLike(String userId, String content, Pageable pageable);
to this:
#Query("{\"bool\": { " +
" \"filter\": [ " +
" { " +
" \"term\": { " +
" \"userId.keyword\": \"?0\" " +
" } " +
" }, " +
" {" +
" \"match\": { " +
" \"content\": \"?1\" " +
" }" +
" } " +
" ] " +
" }}")
Page<SearchablePageHistory> findAllByUserIdAndContentLike(String userId, String content, Pageable pageable);
solved the problem.
Addition:
"ImJvb2wiOiB7ICAgICJmaWx0ZXIiOiBbICAgICB7ICAgICAgICJ0ZXJtIjogeyAgICAgICAidXNlcklkLmtleXdvcmQiOiAiMzFjMjA5NTktNjg5Zi00YjI4LWExNzctNmQ3ZTQ2YTBhYzMwIiAgICAgIH0gICAgIH0sICAgeyJtYXRjaCI6IHsgICAgImNvbnRlbnQiOiAidGVzdHNzIiAgIH19ICAgIF0gICB9"
is a wrapper query, that's a base64 encoded string conataining
""bool": { "filter": [ { "term": { "userId.keyword": "31c20959-689f-4b28-a177-6d7e46a0ac30" } }, {"match": { "content": "testss" }} ] }"
Had similar issue and was actually the absence of bracket
I know a similar question has been posted, however, the answer didn't work for me.
I am new to this RestAssured.I want to get the 'uuid' value if categories equals to 'FUNGI' and feature->features conatains 'VRA'.This is my sample json:
[{
"uuid":"e223d29b-499b-b58b-995e-654bef1aab03",
"categories":[
{
"uuid":"89d1c022-4453-5f6b-883c-46730d429b2a",
"name":"FERTI"
}
],
"minRateSi":0.0
},
{
"uuid":"93015a1b-76ac-2ca3-2bbc-5ae5480962c2",
"categories":[
{
"uuid":"61c951b1-3e47-f0a0-80d8-3d43efa339fb",
"name":"FUNGI"
}
],
"minRateSi":0.0,
"maxRateSi":7.5E-8,
"features":[
{
"id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
"vraMinRate":2.0E-8,
"features":[
"VRA"
]
},
{
"id":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
"vraMinRateSi":3.0E-8,
"features":[
"VRA"
]
}
]
},
{
"uuid":"38290452-4937-4f33-c54d-7f502b84ed99",
"categories":[
{
"uuid":"2c9d8cc0-01bc-899d-6782-cf412e90fd78",
"name":"FUNGI"
}
],
"maxRateSi":1.0E-7,
"features":[
{
"id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
"vraMinRateSi":6.5E-8
},
{
"cropUuid":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
"vraMinRateSi":5.0E-8
}
]}]
Woo , okay your case was pretty hard to break down, but as far as you know how to use JSONArray and JSONObject classes you will be fine.
First of all let me mention how you should approach this kind of scenarios.
I prefer using an online JSON Formatter like this one simply paste your JSON payload and you will be able to tell whether to use JSONObject or JSONArray.
Please take a note here, and check out how to parse json payloads
This code will work for you, although you need to find out the logic behind it and implement JSON parsing accordingly in your future cases.
//declaring your payload
String myJsonPayload = "[{\r\n" + " \"uuid\":\"e223d29b-499b-b58b-995e-654bef1aab03\",\r\n" +
" \"categories\":[\r\n" + " {\r\n" +
" \"uuid\":\"89d1c022-4453-5f6b-883c-46730d429b2a\",\r\n" +
" \"name\":\"FERTI\"\r\n" + " }\r\n" + " ],\r\n" +
" \"minRateSi\":0.0\r\n" + " },\r\n" + " {\r\n" +
" \"uuid\":\"93015a1b-76ac-2ca3-2bbc-5ae5480962c2\",\r\n" + " \"categories\":[\r\n" +
" {\r\n" + " \"uuid\":\"61c951b1-3e47-f0a0-80d8-3d43efa339fb\",\r\n" +
" \"name\":\"FUNGI\"\r\n" + " }\r\n" + " ],\r\n" +
" \"minRateSi\":0.0,\r\n" + " \"maxRateSi\":7.5E-8,\r\n" + " \"features\":[\r\n" +
" {\r\n" + " \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
" \"vraMinRate\":2.0E-8,\r\n" + " \"features\":[\r\n" +
" \"VRA\"\r\n" + " ]\r\n" + " },\r\n" + " {\r\n" +
" \"id\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
" \"vraMinRateSi\":3.0E-8,\r\n" + " \"features\":[\r\n" +
" \"VRA\"\r\n" + " ]\r\n" + " }\r\n" + " ]\r\n" + " },\r\n" +
" {\r\n" + " \"uuid\":\"38290452-4937-4f33-c54d-7f502b84ed99\",\r\n" +
" \"categories\":[\r\n" + " {\r\n" +
" \"uuid\":\"2c9d8cc0-01bc-899d-6782-cf412e90fd78\",\r\n" +
" \"name\":\"FUNGI\"\r\n" + " }\r\n" + " ],\r\n" +
" \"maxRateSi\":1.0E-7,\r\n" + " \"features\":[\r\n" + " {\r\n" +
" \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
" \"vraMinRateSi\":6.5E-8\r\n" + " },\r\n" + " {\r\n" +
" \"cropUuid\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
" \"vraMinRateSi\":5.0E-8\r\n" + " }\r\n" + " ]}]";
JSONArray json = new JSONArray(myJsonPayload);
System.out.println(json);
for (int i = 0; i < json.length(); i++) {
JSONObject object = (JSONObject) json.get(i);
// System.out.println(object); // you have each JSONObject { } contained in the
// Outer JSONArray [ ]
//Getting both uuid's since you didn't answer which one.
String uuid = object.getString("uuid");
System.out.println("\n\n Next Object - Outer UUID: " + uuid);
JSONArray categories = object.getJSONArray("categories");
System.out.println(categories);
if (((JSONObject) categories.get(0)).getString("name").equalsIgnoreCase("fungi")) {
System.out.println("\nFUNGI found!:\n");
//Getting the inner UUID
String uuidOfFungi = ((JSONObject) categories.get(0)).getString("uuid");
System.out.println("Inner UUID: " + uuidOfFungi);
} else {
System.out.println("\nFUNGI NOT FOUND (ABORTING):\n");
}
// Getting Features:
try {
JSONArray featuresObject = object.getJSONArray("features");
for (int index = 0; index < featuresObject.length(); index++) {
try {
JSONObject features = featuresObject.getJSONObject(index);
//Getting VRA
String vraFeatues = features.getJSONArray("features").getString(0);
} catch (JSONException e) {
System.out.println("No inner JSONArray in features JSONObject");
}
}
} catch (JSONException e) {
System.out.println("JSON OBJECT " + i + " HAS NO FEATURES ARRAY");
}
}
Depending again on your use-case it's up to you how you will get the JSON payload into your program.
Getting it from remote URL using Java
Passing it as an argument on java -jar execution time , which i don't really recommend
Either case, modifications need to be done in order to initialize myJsonPayload accordingly.
Hope it helped!
I need to create and update index mapping using jest and transport client , but when i try with this tutorial https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html#java-admin-indices-create-index-settings
i have this problem:
Multiple markers at this line
- Syntax error on token ")", delete this token
- Syntax error on token ".", # expected after
This is my code:
Client client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
IndicesAdminClient indicesAdminClient = client.admin().indices();
indicesAdminClient.prepareCreate("calls")
.setSettings(Settings.builder()
.put("index.number_of_shards", 10)
)
.addMapping("call", "{\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"string\"},\n" +
" \"number\": {\n" +
" \"type\": \"string\"},\n" +
" \"name\": {\n" +
" \"type\": \"string\"}\n" +
" }\n" +
" }")
.get();
String json = "{" +
"\"id\":\"1\"," +
"\"number\":\"123333333\"," +
"\"name\":\"Sharon Tries Elastic\"" +
"}";
IndexResponse response = client.prepareIndex("calls", "call")
.setSource(json)
.get();
// Index name
String _index = response.getIndex();
// Type name
String _type = response.getType();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
}
can help please
thank you
I have a JSON file, as String:
String compString = "{\n" +
" \"Component\": {\n" +
" \"name\": \"Application\",\n" +
" \"environment\": \"QA\",\n" +
" \"hosts\": [\n" +
" \"box1\",\n" +
" \"box2\"\n" +
" ],\n" +
" \"directories\": [\n" +
" \"/path/to/dir1/\",\n" +
" \"/path/to/dir2/\",\n" +
" \"/path/to/dir1/subdir/\",\n" +
" ]\n" +
" }\n" +
" }";
I have a bean representing it (correct if incorrectly)
public class Component {
String name;
String environment;
List<String> hosts = new ArrayList<String>();
List<String> directories = new ArrayList<String>();
// standard getters and setters
}
I am trying to feed this String to this class by:
Gson gson = new Gson();
Component component = gson.fromJson(compString, Component.class);
System.out.println(component.getName());
Above does not work. (I am getting null back, as if Component's name value is never set)
What am i missing please?
In fact, you have to remove the enclosing class from Json.
Indeed, JSON begins with the content of the enclosing class.
So your JSON would be:
String compString = "{\n" +
" \"name\": \"Application\",\n" +
" \"environment\": \"QA\",\n" +
" \"hosts\": [\n" +
" \"box1\",\n" +
" \"box2\"\n" +
" ],\n" +
" \"directories\": [\n" +
" \"/path/to/dir1/\",\n" +
" \"/path/to/dir2/\",\n" +
" \"/path/to/dir1/subdir/\",\n" +
" ]\n" +
" }\n";
String compString =
" {\n" +
" \"name\": \"Application\",\n" +
" \"environment\": \"QA\",\n" +
" \"hosts\": [\n" +
" \"box1\",\n" +
" \"box2\"\n" +
" ],\n" +
" \"directories\": [\n" +
" \"/path/to/dir1/\",\n" +
" \"/path/to/dir2/\",\n" +
" \"/path/to/dir1/subdir/\",\n" +
" ]}" ;
I think you should read more about json, I have removed something in your json string and then success.