How can i convert a json object from one structure to another in apache camel using java?
I have tried using jsonpath but I'm not sure if i am going in the right direction or how it's going to look at in the end.
ReadContext ctx = JsonPath.parse(json);
LinkedHashMap full = ctx.read("$");
String fulldata = full.toString()
.replace("date_of_birth", "birth")
.replace("name", "person_name")
.replace("full_name", "fullname")
.replace("last_name", "last")
.replace("middle_name", "middle")
.replace("first_name", "first")
This is how my json looks like now
"person": {
"country_of_residence": [
"USA"
],
"document_number": "1",
"document_type": "passport",
"full_name": {
"last_name": "John",
"middle_name": "Jack",
"first_name": "Dan"
}
"document_expiration_date": "2020-12-03",
}
This is what I am trying to achieve
{
"label": [
"user"
],
"property": {
"person_name": "name",
"fullname": {
"last": "John",
"middle": "Jack",
"first": "Dan"
}
}
"id": {
"expiry_date": "2020-12-03",
}
Related
I am trying to transform the below JSON:
From:
{
"id": 123,
"name": {
"firstName": "shiva",
"lastName": "kumar"
},
"dateOfBirth": "11/09/2012",
"emailId": "mymail#gmail.com",
"address": {
"addressLine1": "blr",
"addressLine2": "KA"
},
"salary": 12334
}
to:
{
"id": 123,
"email": "mymail#gmail.com",
"salary": 12334,
"fullname": {
"firstName": "shiva",
"lastName": "kumar"
},
"fullAdress": "blr KA"
}
Code: The below code just return null.
private JSONObject tranformRequest(String json, String specFile){
URI uri = Thread.currentThread().getContextClassLoader().getResource(specFile).toURI();
String jsonSpec = Files.readAllLines(Paths.get(uri)).stream().collect(Collectors.joining());
JSONArray jsonArray = new JSONArray(jsonSpec);
List<Map<String, Object>> list = new ArrayList<>();
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i).toMap());
}
Chainr chainr = Chainr.fromSpec(list);// JsonUtils.classpathToList( "/path/to/chainr/spec.json" );
Object output = chainr.transform( json );
return (JSONObject) output;
}
pom.xml
<dependency>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>jolt-core</artifactId>
<version>0.1.7</version>
</dependency>
[
If jolt failed to convert your input JSON return null. So it can be for jolt specification.
Let's use the demo version of the site. Maybe the problem is solved.
Please update your package with version 0.1.1 like the jolt-demo website, and use the below code.
[
{
"operation": "shift",
"spec": {
"id": "&",
"name": "fullname",
"emailId": "email",
"salary": "salary",
"address": {
"*": "&1"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(' ',#0)"
}
}
]
If you have any problem please say in the comment.
I want to get some idea if there is any library in Java that I can use to filter a JSON document based on a predefined JSON mapping document, or only way to achieve this is to write custom java code.
If it's required to write Java custom code what type of design pattern or data structure I should follow. Any advice will be very much appreciated.
Example -
Input JSON document -
{
"basicInfo": {
"name": "name",
"age": "25",
"address": "address"
},
"education": [
{
"ug": {
"unversity": "university",
"major": [
"cs",
"ds"
],
"year": "2012"
},
"pg": {
"unversity": "university",
"major": [
"cs",
"ds"
],
"year": "2015"
}
}
]
}
Predefined JSON mapping document (Can be defined in any JSON format, below is one such example that I have created) -
{
"definitions": {
"basicInfo": {
"maxOccurance": "1",
"fields": [
{
"key": "name",
"type": "S",
"lenght": "50",
"usage": "M",
"maxOccurrance": "1"
},
{
"key": "age",
"type": "S",
"lenght": "3",
"usage": "O",
"maxOccurrance": "1"
}
]
},
"education": {
"maxOccurance": "10",
"fields": [
{
"pg": {
"maxOccurance": "1",
"fields": [
{
"key": "university",
"type": "S",
"lenght": "50",
"usage": "M",
"maxOccurrance": "1"
},
{
"key": "major",
"type": "S",
"lenght": "3",
"usage": "O",
"maxOccurrance": "1"
}
]
}
}
]
}
}
}
Expected output JSON document
{
"basicInfo": {
"name": "name",
"age": "25"
},
"education": [
{
"pg": {
"unversity": "university",
"major": "cs"
}
}
]
}
The request is for a way to filter a JSON document based on a predefined JSON spec sheet / JSON mapping document. The jolt library would likely be able to meet this requirement.
I have a lambda function which takes SNSEvent(http://javadox.com/com.amazonaws/aws-lambda-java-events/1.1.0/com/amazonaws/services/lambda/runtime/events/SNSEvent.html) as the input and then works on that. For writing the some integration test cases, I wanted to call this lambda with the SNSEvent serialized using using lambda.invoke in which the message would be string.
So, for that, I was doing GSON.toJson([object of SNSEvent type]) and from this, I was getting a string. But the problem is, when I am using this string the invoke the lambda, I am getting an exception that deserialization of this string is failing.
Eg. GSON.toJson is deserializing to :
{
"records": [
{
"eventSource": "aws:sns",
"eventVersion": "1.0",
"eventSubscriptionArn": "arn:aws:sns:us-west-2:{{{accountId}}}:ExampleTopic",
"sns": {
"type": "Notification",
"messageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"topicArn": "arn:aws:sns:us-west-2:{{accountId}}:ExampleTopic",
"subject": "example subject",
"message": "example message",
"timestamp": "1970-01-01T00:00:00.000Z",
"signatureVersion": "1",
"signature": "EXAMPLE",
"signingCertUrl": "EXAMPLE",
"unsubscribeUrl": "EXAMPLE",
"messageAttributes": {
"test": {
"type": "String",
"value": "TestString"
},
"testBinary": {
"type": "Binary",
"value": "TestBinary"
}
}
}
}
]
}
while, the actual, it wants is (with capital letters):
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-west-2:{{{accountId}}}:ExampleTopic",
"Sns": {
"Type": "Notification",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"TopicArn": "arn:aws:sns:us-west-2:{{accountId}}:ExampleTopic",
"Subject": "example subject",
"Message": "example message",
"Timestamp": "1970-01-01T00:00:00.000Z",
"SignatureVersion": "1",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"UnsubscribeUrl": "EXAMPLE",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
}
}
}
]
}
Any idea why is it so? And how can I fix it? Should I use some other object instead of SNSEvent while invoking the lambda.
I wanted to parse this structure which is an elasticsearch filter:
{
"filter": {
"name_synonyms_filter": {
"synonym_path": "sample.txt",
"type": "abc_synonym_filter"
},
"name_formatter": {
"name": "name_formatter",
"type": "abc_token_filter"
}
}
}
My question is how can I access individual filters without using key ("name_synonyms_filter" , etc) in java?
your JSON was impropertly formatted.
Here it is fixed:
{
"abc": [{
"name": "somename"
},
{
"name": "somename"
}
]
}
How to parse it:
let x = JSON.parse({
"abc": [{
"name": "somename"
},
{
"name": "somename"
}
]
});
console.log(x);
Let me know if you have any questions.
so I have got a problem with updating object which contain a list of elements. My object definition:
public class Project {
private String _id;
private String name;
private List<Pair> config;
}
And the Pair object:
public class Pair {
private String key;
private String value;
}
I'm using Spring Rest repository to provide the Rest Api and everything is stored in mongodb. Just using the simple interface to create mongoRepository
#RepositoryRestResource(collectionResourceRel = "project", path = "projects")
public interface ProjectRepository extends MongoRepository<Project, String>{
Project findByName(String name);
}
When I create a project object with json (sending POST to /projects):
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
},{
"key": "port2",
"value": "123"
}]
}
I have got the proper response and object has been created:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port2",
"value": "123"
}]
}
So right now I would like to send PUT to update my object and I'm getting strange results:
For example sending following body with PUT to
localhost:8151/projects/58c916fad76a3a186731ad28
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
}]
}
So I want to remove one element from list. The response is (Status OK):
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port2",
"value": "123"
}]
}
So the number of elements didn't change what I expected (my expectations was that the new list replace the old one). Next test:
I would like to add one new element to list:
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
},{
"key": "port1",
"value": "13"
},{
"key": "port2",
"value": "14"
}]
}
Gives following result:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port1",
"value": "13"
}]
}
New element hasn't been added but the second element has changed.
It looks like instead of List mongo save it as an array and can't change the size but can update the element. Am I right?
But, if it would be true the next test should return the same result:
I'm sending the empty list of config and I'm expect that I will have an two-element list.
{
"name": "test_project",
"config": []
}
But what is strange for me I have got the following result:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[]
}
So the number of elements has been updated.
To be honest right now I'm totally confused how it works. Could anyone explain how Spring rest repository handle this action and propose a proper solution for this problem?
I am having the same issue. As a workaround you can send a PATCH request. This updates the array properly.