I am serializing an object to a json.
string output = JsonConvert.SerializeObject(item)
public class Item
{
public int name;
public string category;
}
output
[
{
"Name": "Item1",
"category": "Free",
},
]
in the serialized string I want to have a ref to the schema defined for the json file
output
[
{
"$schema": "http://www.example.com/itemModel.json#",
"Name": "Item1",
"category": "Free",
},
]
Related
I have been struggling with converting the below json .
Is the data class should be mapped as Map<String,Object>.Then use Object mapper class to all values?What is the best way to map below pojo
Json Response
{
"data": {
"Order": [
{
{
"Property1" : Number1
"Propery2": Number 2
},
"Lines": [
{
"Id": "123456",
"itemDescription": "Green glass",
}
],
"date": "",
"updateDate": ""
}
]
}
}
Data class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class Details {
public Map<String,Object> data;
}
This is a VB.Net project. Cannot seem to get the class library correct for deserializing the response string to the classes.
Error we receive when we try to deserialize is:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'blahblah_Framework_Root.blahblah+Tokenscopelist'
because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
Sample response trying to deserialize:
{
"data": {
"expiresAt": "2021-11-27T01:12:05.000Z",
"createdAt": "2021-11-13T01:12:05.000Z",
"identityId": 2347335,
"identity": {
"id": 2347335,
"namespace": {
"id": 7760,
"nickname": "org:random-parking"
}
},
"tokenScopeList": [
{
"org": {
"id": 5995,
"name": "RANDOM Parking",
"opal": "opal:prod:helium:alpha:5995:org:5995",
"parentOrg": null,
"packagePlans": [
{
"id": 50,
"package": {
"id": 14,
"name": "Additional Users Over 500",
"packageFeatures": [
{
"config": null,
"id": 147,
"feature": {
"id": 1,
"name": "Users Included",
"code": "users"
}
}
]
},
"isSelected": true
},
{
"id": 54,
"package": {
"id": 15,
"name": "Elevator I/O Boards",
"packageFeatures": [
{
"config": null,
"id": 175,
"feature": {
"id": 21,
"name": "Elevator I/O Boards",
"code": "elevatorIoBoards"
}
}
]
},
"isSelected": true
},
{
"id": 38,
"package": {
"id": 11,
"name": "Premium",
"packageFeatures": [
{
"config": {
"count": 500
},
"id": 91,
"feature": {
"id": 1,
"name": "Users Included",
"code": "users"
}
},
{
"config": {
"count": 1000000
},
"id": 92,
"feature": {
"id": 2,
"name": "Administrators",
"code": "administrators"
}
}
]
},
"isSelected": true
}
]
},
"user": {
"id": 2502299,
"opal": "opal:prod:helium:alpha:5995:user:2502299"
},
"scope": [
"o5995-admin:r",
"o5995-admin:w",
"o5995-configurations:r",
"o5995-configurations:w",
"o5995-dash:r",
"o5995-dash:w",
"o5995-videoProviderPlayback:w"
]
},
{
"org": {
"id": null,
"name": null
},
"user": {
"id": null,
"opal": null
},
"scope": [
"i2347335:r",
"i2347335:w"
]
}
],
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVw"
},
"meta": {
"accessToken": null
}
}
Using this class definition
Public Class RootobjectLogin
Public Property data As DataumLogin
Public Property meta As Meta
End Class
Public Class DataumLogin
Public Property expiresAt As Date
Public Property createdAt As Date
Public Property identityId As Integer
Public Property identity As Identity
Public Property tokenScopeList() As Tokenscopelist
Public Property token As String
End Class
Public Class Tokenscopelist
Public Property org As Org
Public Property user As User
Public Property scope() As String
End Class
Public Class Packageplan
Public Property id As Integer
Public Property package As Package
Public Property isSelected As Boolean
End Class
Public Class Package
Public Property id As Integer
Public Property name As String
Public Property packageFeatures() As New List(Of Packagefeature)
End Class
Public Class Packagefeature
Public Property config As Config
Public Property id As Integer
Public Property feature As Feature
End Class
Public Class Config
Public Property count As Integer
End Class
Public Class Feature
Public Property id As Integer
Public Property name As String
Public Property code As String
End Class
Public Class User
Public Property id As Integer
Public Property opal As String
End Class
Public Class Org
Public Property id As Integer
Public Property name As String
Public Property opal As String
Public Property parentOrg As Object
Public Property packagePlans() As New List(Of Packageplan)
End Class
Public Class Meta
Public Property accessToken As Accesstoken
End Class
Ideas?
Tools: Spring Booot v2.1.3.RELEASE, MySQL 5.7
I have table with column of type JSON named "properties".
I use jdbcTemplate.queryForList(sql) method to read from this table.
Rest service returns something like this:
[
{
"id": 1,
"name": "users",
"properties": "{\"prop1\": \"value1\"}",
"description": "smpl descr1",
"log_enabled": false
},
{
"id": 2,
"name": "members",
"properties": null,
"description": "sample description 2",
"log_enabled": true
}
]
As you can see the "properties" object is type of String.
How to force jdbcTemplete to convert data from JSON column into JSON instead of String?
Expected result:
[
{
"id": 1,
"name": "users",
"properties": {
"prop1": "value1"
},
"description": "smpl descr1",
"log_enabled": false
},
{
"id": 2,
"name": "members",
"properties": null,
"description": "sample description 2",
"log_enabled": true
}
]
I am sorry that JdbcTemplete does not have such function. You have to convert the JSON string to the java object by yourself using your favourite JSON library.
For example , in case of Jackson , you can convert any JSON string to a Map using:
ObjectMapper mapper = new ObjectMapper();
String json = "{\"prop1\": \"value1\" , \"prop2\": 123}";
Map<String,Object> result = mapper.readValue(json,new TypeReference<Map<String,Object>>() {});
result.get("prop1") // "value1"
result.get("prop2") // 123
I have a json like this, which i am getting in the response from http call
{
"offset": 0,
"limit": 50,
"objects": [
{
"id": "59118fb6e4b0168ec4b56692",
"modifiedDate": 1494323126886,
"requestedIds": null,
"mergedIds": [],
"properties": {
"name": [
{
"value": "Abhimanyu",
"metadata": {}
}
],
"company": [],
"title": [],
"email": [
{
"value": "absinghrathore127#gmail.com",
"metadata": {}
}
]
},
"state": "ACTIVE"
},
{
"id": "590d5813e4b03a8336fa1642",
"modifiedDate": 1494046739619,
"requestedIds": null,
"mergedIds": [],
"properties": {
"name": [
{
"value": "Tim Archer",
"metadata": {}
}
],
"company": [],
"title": [],
"email": [
{
"value": "tim#avocado.com",
"metadata": {}
}
]
},
"state": "ACTIVE"
}
],
"size": 2
}
and i am able to get objects from json via this following code :
String s = res.getBody();
Map<String,Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(s);
String jsonSubset = JSON.serialize(jsonMap.get('objects'));
What i need is the value of name and email in some variable.
Please help me out in this!!
This is going to be a tedious task but once you've classified your all data into appropriate Wrapper classes then it's fairly simple and easy to maintain.
First thing is to define your MainWrapper class. This will contain all the at it's own level. If it has any Object as key-pair then we need to make sure to include it as a List<>. So This is how your MainWrapper should be:
public class MainWrapper {
Integer offset; // Singleton variable
Integer limits; // Singleton variable
List<ObjectsWrapper> objects; // Collection variable since it starts with [],
Integer size; // Singleton variable
}
Since you've array of objects in JSON that's why I've included it as a List in MainWrapper. Now it's time to define ObjectsWrapper. Below is wrapper defined for the same.
public class ObjectsWrapper {
String id;
String modifieddate;
String requestedIds;
PropertyWrapper properties;
}
Since there is only on properties associated with objects that's why it's a non-collection type. Below is representation of properties.
public class PropertyWrapper {
List<NameWrapper> name;
List<String> company;
List<String> title;
List<EmailWrapper> email;
String state;
}
public class NameWrapper {
String name;
String metadata;
}
I guess now you've a fair idea of how to organize data of JSON into various wrapper class. Once you're done with this, simply deserialize the JSON into MainWrapper class and access it.
For example:
MainWrapper mainJSONWrapper = (MainWrapper) JSON.deserialize(JSON,MainWrapper.class);
List<ObjectsWrapper> objectsLst = mainJSONWrapper.objects;
for(ObjectsWrapper obj:objectsLst) {
List<NameWrapper> lstNameWrapper = obj.properties;
for(NameWrapper nameObj:NameWrapper) {
System.debug('Name:'+nameObj.name);
System.debug('metadata:'+nameObj.metadata);
}
}
Above code is not tested but yes, it will give idea how you should deserialize JSON in appropriate manner.
Also go through this answer..How to deserialize a JSON String to Apex
i have a JSON response from remote server in this way:
{
"string": [
{
"id": 223,
"name": "String",
"sug": "string",
"description": "string",
"jId": 530,
"pcs": [{
"id": 24723,
"name": "String",
"sug": "string"
}]
}, {
"id": 247944,
"name": "String",
"sug": "string",
"description": "string",
"jlId": 531,
"pcs": [{
"id": 24744,
"name": "String",
"sug": "string"
}]
}
]
}
In order to parse the response, to list out the "name" & "description", i have written this code out:
interface MyObj {
name: string
desc: string
}
let obj: MyObj = JSON.parse(data.toString());
My question is how do i obtain the name and description into a list that can be displayed.
You gave incorrect type to your parsed data. Should be something like this:
interface MyObj {
name: string
description: string
}
let obj: { string: MyObj[] } = JSON.parse(data.toString());
So it's not MyObj, it's object with property string containing array of MyObj. Than you can access this data like this:
console.log(obj.string[0].name, obj.string[0].description);
Instead of using anonymous type, you can also define interface for it:
interface MyRootObj {
string: MyObj[];
}
let obj: MyRootObj = JSON.parse(data.toString());