Customize the PrettyPrint options in Jackson? - java

I'm aware of the writerWithDefaultPrettyPrinter option in Jackson, but is there any way to customize it? See examples below.
If this isn't possible in Jackson, if you can't change pretty print options, then is there another popular JSON library that would do it?
Summary of options to change:
Don't open multiple containers on the same line
Don't close and open containers on the same line
Use 4 spaces as indents instead of 2
(another option, though I wouldn't use it) Open containers on a new line so that they line up vertically with their closing marker
Example of what it outputs now:
[ {
"id" : "12",
"payload" : [ {
"name" : "url",
"value" : [ {
"name" : "url",
"value" : "http://foobar.com"
} ]
}, {
"name" : "tags",
"value" : [ {
"name" : "tags",
"value" : "red"
}, {
"name" : "tags",
"value" : "green"
}, {
"name" : "tags",
"value" : "blue"
}, {
...
Example of what I'd like to get:
[
{
"id" : "12",
"payload" : [
{
"name" : "url",
"value" : [
{
"name" : "url",
"value" : "http://foobar.com"
}
]
},
{
"name" : "tags",
"value" : [
{
"name" : "tags",
"value" : "red"
},
{
"name" : "tags",
"value" : "green"
},
{
"name" : "tags",
"value" : "blue"
},
{
...

Related

How to update multiple fields(array field and normal field) in a single document of mongo db at a time using JAVA?

I want to update request fields only in an array using java.This is my existing document in mongo db:
{
"_id": "6691e5068dwe335w42cb0a699650f",
"Opportunity_Owner": "Self",
"Account_Name": "IC",
"Lead_Source": "Callbox",
"Opportunity_Name": "name1 ",
"Stage": "Proposal",
"Stage_Status": "A",
"1555570551211": [],
"1555556165153": [],
"1555556059584": [{
"id": "1557389940585",
"Notes": "Note 1"
},
{
"id": "1557389945398",
"Notes": "Hi Bobby "
},
{
"id": "1557389978181",
"Notes": "Spoken to Bobby."
},
{
"id": "1557389990159",
"Notes": "plan to call on 29/Apr"
}
],
"createdBy": "2c18b8dbb7d74a41a66f53a90117480a",
"createdDate": "1562911250917"
}
Request payload:
{
"_id" : "6691e5068dwe335w42cb0a699650f",
"Stage_Status" : "I",
"1555556059584" : [
{
"id" : "1557389940585",
"Notes" : "updated note 123"
}
]
}
I am trying to update "Stage_Status" and "1555556059584.Notes" at a time using $set.I am able to update "Stage_Status" but "1555556059584" array is going to reset with what i have updated with last one.
expected output:
{
"_id" : "6691e5068dwe335w42cb0a699650f",
"Opportunity_Owner" : "Self",
"Account_Name" : "IC",
"Lead_Source" : "Callbox",
"Opportunity_Name" : "name1 ",
"Stage" : "Proposal",
"Stage_Status" : "I",
"1555570551211" : [],
"1555556165153" : [],
"1555556059584" : [
{
"id" : "1557389940585",
"Notes" : "updated note 123"
},
{
"id" : "1557389945398",
"Notes" : "Hi Bobby "
},
{
"id" : "1557389978181",
"Notes" : "Spoken to Bobby."
},
{
"id" : "1557389990159",
"Notes" : "plan to call on 29/Apr"
}
],
"createdBy" : "2c18b8dbb7d74a41a66f53a90117480a",
"createdDate" : "1562911250917"
}
can any one please help me to figure it out in java.
I guess you wanted to update Stage_Status and 1555556059584.Notes at Once .
here is a demo about it
> db.student.find()
{ "_id" : ObjectId("5d2c09ea8ed60ae70d3dd76b"), "name" : "bigbang", "courses" : [ { "name" : "en", "classRoom" : "9001" }, { "name" : "math", "classRoom" : "1001" } ] }
> db.student.update({name:'bigbang','courses.name':'en'},{ $set: {'courses.$.classRoom':'1009',name :"course"} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5d2c09ea8ed60ae70d3dd76b"), "name" : "course", "courses" : [ { "name" : "en", "classRoom" : "1009" }, { "name" : "math", "classRoom" : "1001" } ] }
the java demo is like this
collection.updateOne(and(eq("Stage_Status","A"),eq("1555556059584.id","1557389940585")),new Document("$set" ,new Document("Stage_Status","YOUR_NEW_VALUE").append("1555556059584.$.Notes","YOUR_NEW_VALUE")));
you must set the 1555556059584.id to let the diver know which element to be update .

mongodb update not working on nested subdocument

My mongodb records are like in this link Updating nested array inside array mongodb and sample records are as below and want to update a field in the nested document "parameter" array provided it satisfies some conditions (_id : "04", operations._id : "100" and operations.parameters.pid : "012"), this update query UPDATES wrong nested record (operations.parameters.pid : '011') , please help where I am going wrong:
{
"_id" : "04",
"name" : "test service 4",
"id" : "04",
"version" : "0.0.1",
"title" : "testing",
"description" : "test",
"protocol" : "test",
"operations" : [
{
"_id" : "99",
"oName" : "test op 52222222222",
"sid" : "04",
"name" : "test op 52222222222",
"oid" : "99",
"description" : "testing",
"returntype" : "test",
"parameters" : [
{
"oName" : "Param1",
"name" : "Param1",
"pid" : "011",
"type" : "582",
"description" : "testing",
"value" : "",
"version" : 1.0
},
{
"oName" : "Param2",
"name" : "Param2",
"pid" : "012",
"type" : "58222",
"description" : "testing",
"value" : "",
"version" : 2.0
}
]
},
{
"_id" : "100",
"oName" : "test op 909090",
"sid" : "05",
"name" : "test op 90909",
"oid" : "1009",
"description" : "testing",
"returntype" : "test",
"parameters" : [
{
"oName" : "Param1",
"name" : "Param1",
"pid" : "011",
"type" : "582",
"description" : "testing",
"value" : "",
"version" : 1.0
},
{
"oName" : "Param2",
"name" : "Param2",
"pid" : "012",
"type" : "58222",
"description" : "testing",
"value" : "",
"version" : 2.0
}
]
},
{
"_id" : "101",
"oName" : "test op 52222222222",
"sid" : "04",
"name" : "test op 52222222222",
"oid" : "99",
"description" : "testing",
"returntype" : "test",
"parameters" : [
{
"oName" : "Param1",
"name" : "Param1",
"pid" : "011",
"type" : "582",
"description" : "testing",
"value" : "",
"version" : 1.0
},
{
"oName" : "Param2",
"name" : "Param2",
"pid" : "012",
"type" : "58222",
"description" : "testing",
"value" : "",
"version" : 1.0
}
]
},
{
"_id" : "102",
"oName" : "test op 909090",
"sid" : "05",
"name" : "test op 90909",
"oid" : "1009",
"description" : "testing",
"returntype" : "test",
"parameters" : [
{
"oName" : "Param1",
"name" : "Param1",
"pid" : "011",
"type" : "582",
"description" : "testing",
"value" : "",
"version" : 1.0
},
{
"oName" : "Param2",
"name" : "Param2",
"pid" : "012",
"type" : "58222",
"description" : "testing",
"value" : "",
"version" : 2.0
}
]
}
]
}
My update query is as follows :
db.foo.update(
{ $and : [{'_id':'04'},
{'operations._id':'100' },
{'operations.parameters.pid': '012'}]},
{
"$set": {
"operations.1.parameters.$.dummy": "foo"
}
}
)
I am using mongodb 3.6.2 referred to https://docs.mongodb.com/master/reference/operator/update/positional-filtered/
Sample record from this link :
{
"_id" : 1.0,
"grades" : [
{
"type" : "quiz",
"questions" : [
10.0,
8.0,
5.0
]
},
{
"type" : "quiz",
"questions" : [
8.0,
9.0,
6.0
]
},
{
"type" : "hw",
"questions" : [
5.0,
4.0,
3.0
]
},
{
"type" : "exam",
"questions" : [
25.0,
10.0,
23.0,
0.0
]
}
]
}
Example from this link
db.student3.update(
{},
{ $inc: { "grades.$[t].questions.$[score]": 2 } },
{ arrayFilters: [ { "t.type": "quiz" } , { "score": { $gte: 8 } } ], multi: true}
)
ERror I got from robo-3t :
cannot use the part (grades of grades.$[t].questions.$[score]) to traverse the element ({grades: [ { type: "quiz", questions: [ 10.0, 8.0, 5.0 ] }, { type: "quiz", questions: [ 8.0, 9.0, 6.0 ] }, { type: "hw", questions: [ 5.0, 4.0, 3.0 ] }, { type: "exam", questions: [ 25.0, 10.0, 23.0, 0.0 ] } ]})
Please help;
Regards
Kris
In your update operation
"$set": {
"operations.1.parameters.$.dummy": "foo"
}
refers to the 1st element in operations that is an element with "_id" : "100", and within parameters array, the $ updates the first element in array.
You need to consider using mongodb 3.6 if you want to update nested array elements using $[] to update all matching elements.
One possible way to do this in 3.4 version is fetching the required sub-document and do matches and updates on the application side.

How to map two datasets in spark Java

Hi I'm reading data from mongodb into spark application.
My mongodb contains 2 collections.
One is profile_data(actual data with field names)
(Which holds all the input data including some unique fields)
{
"MessageStatus" : 2,
"Origin" : 1,
"_id" : ObjectId("596340fe8b0fa35d2880db1a"),
"accerlation" : 19.4,
"cylinders" : 4,
"displacement" : 119,
"file_id" : ObjectId("59633e48b760e7c8071a6c1c"),
"horsepower" : 82,
"modelyear" : 82,
"modified_date" : ISODate("2017-07-10T08:47:01.641Z"),
"mpg" : 31,
"snet_id" : "new_project",
"unique_id" : "784",
"username" : "chevy s-10",
"weight" : 2720
}
And another collection is : predictive_model_details(Which holds the ML model details like model name, feature fields and prediction field just like metadata)
{
"_id" : ObjectId("56b4351be4b064bb19a90324"),
"algorithm_id" : "55d717a53d9e22022ff2a1e9",
"algorithm_name" : "K- Nearest Neighbours (IBK)",
"client_id" : "562e1d51b760d0e408151b91",
"feature_fields" : [
{
"name" : "Origin",
"type" : "int"
},
{
"name" : "accerlation",
"type" : "Double"
},
{
"name" : "displacement",
"type" : "Int"
},
{
"name" : "horsepower",
"type" : "Int"
},
{
"name" : "modelyear",
"type" : "Int"
}
],
,
"makeActiveStatus" : "0",
"model_name" : "test1",
"parameter_type" : "system_defined",
"parameters" : [
{
"symbol" : "-K",
"value" : "1"
}
],
"predictor" : {
"name" : "mpg"
"type" : "Int"
},
"result_exists" : true,
"snet_id" : "new_project"
}
So I've created 2 datasets in spark for two collections in MongoDB. Now I want to map these 2 Datasets with all feature fields together and prediction field together.
And common field in 2 datasets is snet_id.
Could anyone please help?

Representing Abstract JSON Objects as models in Java

Ok so I am making API requests to retrieve certain things like movies, songs, or to ping the server. However all of these responses are contained within the same response JSON object that has varying fields depending on the response. Below are three examples.
ping
{
"response" : {
"status" : "ok",
"version" : "0.9.1"
}
}
getIndexes
{
"response" : {
"status" : "ok",
"version" : "0.9.1",
"indexes" : {
"index" : [ {
"name" : "A",
"movie" : [ {
"id" : "150",
"name" : "A Movie"
}, {
"id" : "2400",
"name" : "Another Movie"
} ]
}, {
"name" : "S",
"movie" : [ {
"id" : "439",
"name" : "Some Movie"
}, {
"id" : "209",
"name" : "Some Movie Part 2"
} ]
} ]
}
}
}
getRandomSongs
{
"response" : {
"status" : "ok"
"version" : "0.9.1"
"randomSongs" : {
"song": [ {
"id" : "72",
"parent" : "58",
"isDir" : false,
"title" : "Letter From Yokosuka",
"album" : "Metaphorical Music",
"artist" : "Nujabes",
"track" : 7,
"year" : 2003,
"genre" : "Hip-Hop",
"coverArt" : "58",
"size" : 20407325,
"contentType" : "audio/flac",
"suffix" : "flac",
"transcodedContentType" : "audio/mpeg",
"transcodedSuffix" : "mp3",
"duration" : 190,
"bitRate" : 858,
"path" : "Nujabes/Metaphorical Music/07 - Letter From Yokosuka.flac",
"isVideo" : false,
"created" : "2015-06-06T01:18:05.000Z",
"albumId" : "2",
"artistId" : "0",
"type" : "music"
}, {
"id" : "3135",
"parent" : "3109",
"isDir" : false,
"title" : "Forty One Mosquitoes Flying In Formation",
"album" : "Tame Impala",
"artist" : "Tame Impala",
"track" : 4,
"year" : 2008,
"genre" : "Rock",
"coverArt" : "3109",
"size" : 10359844,
"contentType" : "audio/mpeg",
"suffix" : "mp3",
"duration" : 258,
"bitRate" : 320,
"path" : "Tame Impala/Tame Impala/04 - Forty One Mosquitoes Flying In Formation.mp3",
"isVideo" : false,
"created" : "2015-06-29T21:50:16.000Z",
"albumId" : "101",
"artistId" : "30",
"type" : "music"
} ]
}
}
}
So basically my question is, how should I structure my model classes to use for parsing these responses? At the moment I have an abstract response object that only contains fields for the status and version. However, by using this approach I will need a response class that extends this abstract class for ever request I make (e.g. AbstractResponse, IndexesResponse, RandomSongsResponse). Also, some models with the same name may have different fields depending on the API request made. I would prefer to avoid making a model class for every possible scenario.
And as an extra note, I am using GSON for JSON serialization/deserialization and Retrofit to communicate with the API.

STS Custom Template

I'm having an issue in STS creating a custom template. What I want is the ability to do token-based replacement within all files in the project. I've read this is possible with the "token" replacement, but cannot seem to get the wizard.json to let me accept multiple inputs. The "example" you see everywhere is replacing the top-level-package, which I've got to work great.
Anyone have any examples of doing multiple inputs in the wizard.json, or have any idea on what I'm doing wrong?
Here is what I have, which doesn't work:
{
"info" : {
"elements" : {
"element" : [
{
"name" : "mysystem",
"description" : "Please the name of your system (ex. marketing, hr, accounting, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
],
"element" : [
{
"name" : "myapp",
"description" : "Please the name of your application (ex. thisapplication, thatapplication, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
]
},
"projectName" : "projectName",
"pages" : {
"page" : [
{
"order" : 0,
"description" : "System Info"
}
]
}
}
}
The "myapp" picks up and does the translation, but the "mysystem" never even gets displayed for input (screen one is just the myapp text).
For those who run into the same problem, I have figured this out. Here is what I ended up with.
{
"info" : {
"elements" : {
"element" : [
{
"name" : "mysystem",
"description" : "Please the name of your system (ex. marketing, hr, accounting, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
},
{
"name" : "myapp",
"description" : "Please the name of your application (ex. thisapplication, thatapplication, etc).",
"type" : "java.lang.String",
"page" : 1,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
]
},
"projectName" : "projectName",
"pages" : {
"page" : [
{
"order" : 0,
"description" : "System Info"
}
],
"page" : [
{
"order" : 1,
"description" : "System Info 2"
}
]
}
}
}

Categories

Resources