Java object created from JSON, update same object with new JSON - java

I have a server that I talk to which I have no control over. I simply consume, and that's it. I get responses from it in the form of JSON. I use GSON to instantiate a Java object from this JSON. Later, I get another response from the server that represents an update that needs to happen to a portion of that object. It could be any number of fields and other member objects that get updated, down through any part of the tree of objects from the main object the JSON represents.
Initially, the object comes in (without formatting) looking something like this:
{
"action": "ephmeral",
"object1": {
"id": 1,
"stuff": [{
"id": 2,
"type": "something",
"where": "there",
"count": 5
}, {
"id" 3,
"type": "otherthing",
"where": "here",
"count": 5
}
],
"field2": 2,
"field3": 3,
"field4": 4,
"field5": 5,
...
"fieldN": 999,
}
}
Which is a small example representing any amount of complexity in the object and its fields. This is the initial state of the main object.
Later on, there is communication to the server to update the object in small way (any number of fields can be updated, this is a small example).
[{
"action": "update",
"object1": {
"id": 1,
"stuff": [{
"id": 2,
"count": 10
}
]
}
}, {
"action": "delete",
"object1": {
"id": 1,
"stuff": [{
"id": 3
}
]
}
}, {
"action": "update",
"object2": {
"id": 5,
"amount": 123.45
}
}
]
Those action tags denote a specific way the object or any of its fields should be updated (well, were updated server side already) in some way. It could be updating things, deleting things, etc. as per the value of action.
My question is, not being familiar with JSON and REST, and some of the other technologies the server uses (it's using Rails for example; I'm using Java), I'm not even sure how to go about searching to see if there is a name to this particular way of doing things that I can then find a library for to integrate into my Java code. Alternatively, is there some way that GSON can handle this properly? As it stands, it gives me a new object representing the first action, and none of the subsequent actions affect the object (I'm not thinking of this as a bug or anything wrong with GSON).
At the moment I'm parsing the JSON response string myself for known, fixed (ish) responses for a few, small, critical requests. But this is, of course, not at all dynamic or flexible. Is this a custom communication style, or is it a known style that I can get handled flexibly and dynamically?

Related

Extract subset of json from given JsonPath

I have a nested json structure and a list of json paths. I want to extract and return only those parts of json that are defined in the json paths.
For eg: Let's say the json contains:
{
"id": 1,
"user": {
"name": "ABCD",
"email": "abcd#email.com",
"phone": "1234"
}
}
And I've the following JsonPath's
$.id
$.user.name
$.user.phone
So the json returned will be
{
"id": 1,
"user": {
"name": "ABCD",
"phone": "1234"
}
}
I've tried multiple ways with Jayway's JsonPath but I only get the value, not the entire heirarchy(Eg: If I search for $.user.name then I get ABCD instead of {"user": {"name": "ABCD"}}).
I looked at Extracting a subset of attributes with JSONPath but it does not answer the question of return entire json structure.
Any ideas how do I approach this? I suspect that I would need to build Jackson JsonNode iteratively using the JsonPath, but couldn't build a solution.
Note: The example above contains a simple json structure. In reality, I suspect to have many nested structures(even arrays) inside json.

Is is possible to reduce two separate query to only one in Elastic Search?

I have ElasticSearch data offers with the following structure:
{
{
"id": "123",
"tariffId": "15477",
"tariffFamilyId": "555",
"characteristics": "xxx"
},
{
"id": "124",
"tariffId": "15478",
"tariffFamilyId": "777",
"characteristics": "yyy"
},
{
"id": "351",
"tariffId": "25271",
"tariffFamilyId": "555",
"characteristics": "zzz"
}
}
I need to find all offers with tariffFamilyId of a certain tariffId. As an initial argument, I know only tariffId and do not know tariffFamilyId (I need to detect it). Normally it means the two separate requests to Elastic Search should be made:
first request - find tariffFamilyId by tariffId.
second request - find offers with that tariffFamilyId.
For example for tariffId=15477, we get tariffFamilyId=555. So for this family, there will be two offers with id 123 and 351.
The question - is it possible to somehow make only one request to Elastic search, not two?
P.S. This is for Java implementation.

Extract and Crete new JSON string

I have some spring MVC based APIs which produces JSON/ XML which represents API output.
{
"data" :
{
"users": [
{
"id": "001",
"name: "abc1",
"type": {
"id": "P",
"name": "Permanent"
}
},
{
"id": "002",
"name: "xyz",
"type": {
"id": "C",
"name": "Contractor"
}
}
]
}
}
I'm passing a parameter with request as
url?fields=users.id, users.type.id
users.type.id is a sub-node in users node.
users node is an array.
Now, what I want to do is to filter those only properties and create the response based upon fields passed in the request.
So the response to above filter condition should be same structure and will only contain wanted fields with values.
I'm trying to build a flat map with keys with a dot notation so I won't lose the track to filter, then I'll rebuild the JSON again. I feel this approach is just unreasonable because Jackson has the .path and .with API's to check existing path. But the real challenge is to extract and create a new JSON which matches the response JSON.
I'm looking for some ideas to achieve this. I don't want to try any third party libs btw. I know some libs are there. I want to prefer Jackson way to do this.
Feel free to add or comment if you have a further ideas.

ES JAVA API not able to updateByQuery when adding sort by an existing field

I want to avoid reindexing all documents after a field mapping being changed from (excerpt from the template file):
"subject": {
"type": "string"
},
to
"subject": {
"type": "string",
"fields": {
"sort": {
"type": "string",
"analyzer": "ducet_sort_subject"
},
"sortConversation": {
"type": "string",
"analyzer": "ducet_sort_subject_conversation"
}
}
},
the java code latest version is the following:
PutMappingResponse putMappingResponse = client.admin().indices().preparePutMapping(indexName)
.setSource("{\"properties\": {}}").setType("email").execute().actionGet();
BulkIndexByScrollResponse bulkIndexByScrollResponse = INSTANCE.newRequestBuilder(client).source(indexName).get();
Please note that there is no unmappedType default, so if the mapping is not found, I'm expecting to see some error, (as it does without those lines).
Instead I get the results back also for unindexed documents, but the order is random.
I have also tried to add the mapping manually in the setSource, but result didn't change.
In the documentation I read two statements that seem to be contrasting:
"The simplest usage of _update_by_query just performs an update on every document in the index without changing the source. This is useful to pick up a new property or some other online mapping change."
and
"Templates are only applied at index creation time. Changing a template will have no impact on existing indices. When using the create index API, the settings/mappings defined as part of the create index call will take precedence over any matching settings/mappings defined in the template."
is it even possible to do what I want without reindexing?

How to create a service using Google cloud endpoint which take a json value in input parameter

I wanted to create a service which can accept JSON as an input to my endpoint. eg.
[{ "name": "Ram","event": "processed" },
{"name": "Raj", "event": "click" }] .
Tried some of the possible ways, cloud endpoints are not supporting arrays or any array related types to use. I tried it to convert the json to string but it's not working for me.
Thanks in advance.
You seem to have an invalid JSON. Try validating it here. As you can see, you will encounter a parsing error.
Change all occurrences of “ with "
[
{
"name": "Ram",
"event": "processed"
},
{
"name": "Raj",
"event": "click"
}
]
and now you have a valid JSON.
More details on JSON can be found here.

Categories

Resources