I am consuming a rest-end point using rest template which returns below josn messages.
object Wrappers is better approach or json object parsing is better ?
[
{
"id": "UAMwcI",
"messageId": "PyXPTk-6XUBhbONWZ2DOw3___pFykwV5bQ",
"timestamp": 1574330759814,
"type": "MESSAGESENT",
"initiator": {"user": {
"userId": 345315370604341,
"firstName": "Sanjeet",
"lastName": "Mohanty",
"displayName": "Sanjeet Mohanty",
"email": "Sanjeet.mohanty#db.com",
"username": "Sanjeet.mohanty#db.com"
}},
"payload": {"messageSent": {"message": {
"messageId": "PyXPTk-6XUBhbONWZ2DOw3___pFykwV5bQ",
"timestamp": 1574330759814,
"message": "<div data-format=\"PresentationML\" data-version=\"2.0\" class=\"wysiwyg\"><p>test 2 <\/p><\/div>",
"data": "{}",
"user": {
"userId": 345315370604341,
"firstName": "Sanjeet",
"lastName": "Mohanty",
"displayName": "Sanjeet Mohanty",
"email": "Sanjeet.mohanty#db.com",
"username": "Sanjeet.mohanty#db.com"
},
"stream": {
"streamId": "cL7jzEDwLxX_gN7D1QbMQn___pP73Ad6dA",
"streamType": "IM"
},
"externalRecipients": false,
"userAgent": "DESKTOP-40.0.0-10726-Windows-7-Chrome-76.0.3809.87",
"originalFormat": "com.symphony.messageml.v2"
}}}
},
{
"id": "Bvua7t",
"messageId": "8usJ_xioHKkRrevhyn38wn___pFykv-WbQ",
"timestamp": 1574330761321,
"type": "MESSAGESENT",
"initiator": {"user": {
"userId": 345315370604341,
"firstName": "Sanjeet",
"lastName": "Mohanty",
"displayName": "Sanjeet Mohanty",
"email": "Sanjeet.mohanty#db.com",
"username": "Sanjeet.mohanty#db.com"
}},
"payload": {"messageSent": {"message": {
"messageId": "8usJ_xioHKkRrevhyn38wn___pFykv-WbQ",
"timestamp": 1574330761321,
"message": "<div data-format=\"PresentationML\" data-version=\"2.0\" class=\"wysiwyg\"><p>test 3<\/p><\/div>",
"data": "{}",
"user": {
"userId": 345315370604341,
"firstName": "Sanjeet",
"lastName": "Mohanty",
"displayName": "Sanjeet Mohanty",
"email": "Sanjeet.mohanty#db.com",
"username": "Sanjeet.mohanty#db.com"
},
"stream": {
"streamId": "cL7jzEDwLxX_gN7D1QbMQn___pP73Ad6dA",
"streamType": "IM"
},
"externalRecipients": false,
"userAgent": "DESKTOP-40.0.0-10726-Windows-7-Chrome-76.0.3809.87",
"originalFormat": "com.symphony.messageml.v2"
}}}
}
]
there is a lot of Liberaiyes to serialize deserialize JSON.
if you have Pojo class match the return value from your rest API
you could use for instance Jackson to do the job
PojoClass mypojo = new ObjectMapper().readValue(JsonString, PojoClass.class);
OR simply converted to JSonNode and iterate over the Nodes
JsonNode jsonNode = new ObjectMapper().readTree(JsonString);
check the link for more info
https://www.baeldung.com/jackson-object-mapper-tutorial
Related
I have the following json which get from endpoint,
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:response",
"urn:scim:schemas:extension:customattrs:2.0:User"
],
"meta": {
"resourceType": "User",
"created": "2011-08-01T18:29:49.793Z",
"lastModified": "2011-08-01T18:29:49.793Z",
"version": "W/\"f250dd84f0671c3\""
},
"userName": "bjensen",
"phoneNumbers": [
{
"value": "555-555-8377",
"type": "work"
}
],
"emails": [
{
"value": "bjensen#example.com",
"type": "work",
"primary": true
}
],
"urn:scim:schemas:extension:customattrs:2.0:User": {
"userName": "bjensen",
"address": ""
}
}
please suggest me to parse the json along with schemas "urn:scim:schemas:extension:customattrs:2.0:User" into java object
I have the following large JSON I receive from a specific REST call. I want to only extract the password strings (highlighted in bold). So I need the values.
ConfigPWD_USER.password, ConfigPWD_ROOT.password, instanceOwnerPassword, databaseUserPassword, fencedUserPassword in java. Guess I will have to search using string regex. Can someone help?
{
"deployment_name": "name1",
"model": {
"model": {
"description": "",
"nodes": [{
"id": "OS Node",
"ptype": "image:OS Node",
"attributes": {
<more json key value pairs>
**"ConfigPWD_USER.password": "<xor>NzozMzA=",
"ConfigPWD_ROOT.password": "<xor>NzozMzA="**
},
"type": "image:OS,
"groups": {},
"locked": []
}, {
"id": "disk for db instance",
"attributes": {
<more json key value pairs>
},
"type": "add disk:1.0.0",
"groups": {},
"locked": []
}, {
"id": "disk for database data",
"attributes": {
<more json key value pair>
},
"type": "disk:1.0.0",
"groups": {},
"locked": []
}, {
"id": "DB Server",
"startsAfter": ["disk for db instance",
"OS Node"
],
"locked": ["instanceMountPoint", "instanceMountPoint", "instanceMountPoint"],
"attributes": {
"instanceMountPoint": "${disk for db instance.MOUNT_POINT}",
<more json key value pair>
**"instanceOwnerPassword": "<xor>NzozMzA=",**
<more json key value pair>
**"fencedUserPassword": "<xor>NzozMzA=",**
},
"type": "DBLUN",
"groups": {}
}, {
"id": "bludbs",
"startsAfter": ["DB Server", "disk for database data"],
"locked": ["instanceName", "dataMountPoint", "instanceName", "dataMountPoint", "instanceName", "dataMountPoint"],
"attributes": {
<many json key value pairs>,
**"databaseUserPassword": "<xor>NzozMzA="**,
<many json key value pairs>
},s
"type": "sript:scrip1.exe",
"groups": {}
}],
"app_type": "application",
"name": "Default DB in Linux",
"patterntype": "vsys",
"links": [{
"id": "HostedOnLink_1",
"source": "disk for db instance",
"target": "OS Node",
"attributes": {},
"type": "HostedOnLink",
"groups": {}
},
{
"id": "HostedOnLink_2",
"source": "disk for database data",
"target": "OS Node",
"attributes": {},
"type": "HostedOnLink",
"groups": {}
}, {
"id": "HostedOnLink_3",
"source": "DB Server",
"target": "OS Node",
"attributes": {},
"type": "HostedOnLink",
"groups": {}
}, {
"id": "HostedOnLink_4",
"source": "bludb",
"target": "OS Node",
"attributes": {},
"type": "HostedOnLink",
"groups": {}
}
],
"locked": false,
"readonly": false,
"version": "1.0",
"patternversion": "1.2.3.0",
"mixinArgs": null
},
"layers": [{
"id": "layer",
"nodes": ["OS Node", "disk for db instance", "disk for database data", "DB Server", "bludb"]
}]
},
<more json key value pairs>,
"organizations": []
}
in groovy there is in-build groovy.json.JsonSlurper and code to find one of the values could be like this:
def json = new groovy.json.JsonSlurper().parseText('''
{
"deployment_name": "name1",
"model": {
"model": {
"description": "",
"nodes": [{
"id": "OS Node",
"ptype": "image:OS Node",
"attributes": {
"ConfigPWD_USER.password": "1<xor>NzozMzA=",
"ConfigPWD_ROOT.password": "1<xor>NzozMzA="
},
"type": "image:OS",
"groups": {},
"locked": []
}, {
"id": "disk for db instance",
"attributes": {
"ConfigPWD_USER.password": "2<xor>NzozMzA=",
"ConfigPWD_ROOT.password": "2<xor>NzozMzA="
},
"type": "add disk:1.0.0",
"groups": {},
"locked": []
}]
}
}
}
''')
json.model.model.nodes.collect{n-> n.attributes."ConfigPWD_USER.password" }
I have a JSON response coming from Hubspot API as following, I am having trouble to parse it into Java POJO for using with RestTemplate.
Since the Json reponse is not in form of json arrays,I am not able to create proper java classes.
Can someone please help?
The JSON response received is as following:
{
"1": {
"vid": 1,
"canonical-vid": 1,
"portal-id": 5017510,
"is-contact": true,
"profile-token": "AO_T-mOzMc0AQx3P50QBw3qJ09A30BWfeQu89iOhW5ADWb6-uIT7m37lYwSTk7ObLEYwwnpxGNM1x9rkivaT-abeu6MegOWO31EUpZ3b56hfFti61ewcGQvC-XocjM2fekCOtK5oyM1C",
"profile-url": "https://app.hubspot.com/contacts/5017510/lists/public/contact/_AO_T-mOzMc0AQx3P50QBw3qJ09A30BWfeQu89iOhW5ADWb6-uIT7m37lYwSTk7ObLEYwwnpxGNM1x9rkivaT-abeu6MegOWO31EUpZ3b56hfFti61ewcGQvC-XocjM2fekCOtK5oyM1C/",
"properties": {
"firstname": {
"value": "Cool"
},
"city": {
"value": "Cambridge"
},
"createdate": {
"value": "1539332326199"
},
"company": {
"value": "HubSpot"
},
"state": {
"value": "MA"
},
"email": {
"value": "coolrobot#hubspot.com"
},
"website": {
"value": "http://www.HubSpot.com"
},
"jobtitle": {
"value": "Robot"
},
"lastmodifieddate": {
"value": "1539332330206"
},
"lastname": {
"value": "Robot (Sample Contact)"
}
}
},
"51": {
"vid": 51,
"canonical-vid": 51,
"portal-id": 5017510,
"is-contact": true,
"profile-token": "AO_T-mNsLUnhG0QdyfV6D_JZhoqldgC_jYOZswkSrsgRW1uvUZpajXSjd-83OtQEdptpio0VAgtGD3_qIOe_4vnijJ8_q6traOskjw5A48d1RhghdFuqiDpgz7pRcuzTrMjsvWF5Oat3",
"profile-url": "https://app.hubspot.com/contacts/5017510/lists/public/contact/_AO_T-mNsLUnhG0QdyfV6D_JZhoqldgC_jYOZswkSrsgRW1uvUZpajXSjd-83OtQEdptpio0VAgtGD3_qIOe_4vnijJ8_q6traOskjw5A48d1RhghdFuqiDpgz7pRcuzTrMjsvWF5Oat3/",
"properties": {
"firstname": {
"value": "Brian"
},
"city": {
"value": "Cambridge"
},
"createdate": {
"value": "1539332326434"
},
"company": {
"value": "HubSpot"
},
"state": {
"value": "MA"
},
"email": {
"value": "bh#hubspot.com"
},
"website": {
"value": "http://www.HubSpot.com"
},
"jobtitle": {
"value": "CEO"
},
"lastmodifieddate": {
"value": "1539332334158"
},
"lastname": {
"value": "Halligan (Sample Contact)"
}
}
}
}
See this link, http://www.jsonschema2pojo.org/ in it you pass the JSON and it mounts the POJO for you, however you want.
I tested with your JSON and generated perfectly.
you can use Jackson to convert JSON response into POJO class.
Sample code:
ObjectMapper mapper = new ObjectMapper();
POJOClass pojoclassObj = mapper.readValue(jsonResponse, POJOClass.class);
I'm parsing a DiagnosticReport from a JSON file and It works fine, but when I try to print the same JSON file throught IParser encode function, the JSON is different to the original. I need to print the same JSON.
Original JSON (String json)
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"text": {
"div": "<div><p><b>Narrative B</b></p></div>"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
First step is parse and the second step is encode and print
DiagnosticReport report = parser.parseResource(DiagnosticReport.class, json);
String encodeJSON = parser.encodeResourceToString(report);
System.out.println(encodeJSON);
And the result is different because the text tag in the Observation is not showed
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
I'm trying this because I have a DiagnosticReport generated by my software and I need print it completely in a JSON file.
Thanks for your help!!
It's not legal to have narrative in a contained resource, nor is it legal to have meta/lastUpdated. There are invariants that prohibit both. Ideally, the parsing software should have thrown an exception, but it's not overly surprising that the serializer has trouble serializing content that's not supposed to be there.
Look at dom-1 and dom-4 in dstu3 or dstu2
I am trying to generate POJOs from the JSON Schema of XMBC.
I do this with jsonschema2pojo.
However, nothing gets generated. It doesn't even bring me an error.
This is a reduced sample json schema I am trying to generate from:
{
"description": "JSON-RPC API of XBMC",
"id": "http://xbmc.org/jsonrpc/ServiceDescription.json",
"methods": {
"Addons.ExecuteAddon": {
"description": "Executes the given addon with the given parameters (if possible)",
"params": [
{
"name": "addonid",
"required": true,
"type": "string"
},
{
"default": "",
"name": "params",
"type": [
{
"additionalProperties": {
"default": "",
"type": "string"
},
"type": "object"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "URL path (must start with / or ?",
"type": "string"
}
]
},
{
"default": false,
"name": "wait",
"type": "boolean"
}
],
"returns": {
"type": "string"
},
"type": "method"
}
},
"notifications": {
"Application.OnVolumeChanged": {
"description": "The volume of the application has changed.",
"params": [
{
"name": "sender",
"required": true,
"type": "string"
},
{
"name": "data",
"properties": {
"muted": {
"required": true,
"type": "boolean"
},
"volume": {
"maximum": 100,
"minimum": 0,
"required": true,
"type": "integer"
}
},
"required": true,
"type": "object"
}
],
"returns": null,
"type": "notification"
}
},
"types": {
"Addon.Content": {
"default": "unknown",
"enums": [
"unknown",
"video",
"audio",
"image",
"executable"
],
"id": "Addon.Content",
"type": "string"
}
},
"version": "6.14.3"
}
I must admin that my knowledge of JSON is very terse, maybe it is just a simple fault of mine. But can anyone help me how I can generate Java objects from such a JSON Schema?
The JSON Schema doesn't support method. JSON schema defines json data structure, it would not be used to define your methods. Most important attribute in JSON schema is properties.
It's good to generate POJO data models from a JSON schema, but not business logic. You can learn the JSON schema from those examples.