How to read JSON that contains field that can have different types
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken
etc for this section? simplified JSON and class structure below. value
is the field that varies in type.
`
"users": [
"id": 461774,
"email": "test@test.co.uk",
"name": "test Student",
"sub_network_ids": [
18307
],
"custom_attributes": [
"id": 2310,
"name": "isReady",
"value": true,
"type": "CustomizableAttribute::Boolean"
,
"id": 2323,
"name": "othernames",
"value": "something",
"type": "CustomizableAttribute::Text"
]
]
`
Code:
public class HivebriteUserList
public List<HivebriteUserRecords> users;
public class HivebriteUserRecords
public String name;
public String email;
public List<HivebriteUserCustomAtt> custom_attributes;
public List<Integer> sub_network_ids;
public class HivebriteUserCustomAtt
public String id;
public String name;
public String value;
apex json callout parser
New contributor
add a comment |Â
up vote
1
down vote
favorite
I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken
etc for this section? simplified JSON and class structure below. value
is the field that varies in type.
`
"users": [
"id": 461774,
"email": "test@test.co.uk",
"name": "test Student",
"sub_network_ids": [
18307
],
"custom_attributes": [
"id": 2310,
"name": "isReady",
"value": true,
"type": "CustomizableAttribute::Boolean"
,
"id": 2323,
"name": "othernames",
"value": "something",
"type": "CustomizableAttribute::Text"
]
]
`
Code:
public class HivebriteUserList
public List<HivebriteUserRecords> users;
public class HivebriteUserRecords
public String name;
public String email;
public List<HivebriteUserCustomAtt> custom_attributes;
public List<Integer> sub_network_ids;
public class HivebriteUserCustomAtt
public String id;
public String name;
public String value;
apex json callout parser
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken
etc for this section? simplified JSON and class structure below. value
is the field that varies in type.
`
"users": [
"id": 461774,
"email": "test@test.co.uk",
"name": "test Student",
"sub_network_ids": [
18307
],
"custom_attributes": [
"id": 2310,
"name": "isReady",
"value": true,
"type": "CustomizableAttribute::Boolean"
,
"id": 2323,
"name": "othernames",
"value": "something",
"type": "CustomizableAttribute::Text"
]
]
`
Code:
public class HivebriteUserList
public List<HivebriteUserRecords> users;
public class HivebriteUserRecords
public String name;
public String email;
public List<HivebriteUserCustomAtt> custom_attributes;
public List<Integer> sub_network_ids;
public class HivebriteUserCustomAtt
public String id;
public String name;
public String value;
apex json callout parser
New contributor
I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken
etc for this section? simplified JSON and class structure below. value
is the field that varies in type.
`
"users": [
"id": 461774,
"email": "test@test.co.uk",
"name": "test Student",
"sub_network_ids": [
18307
],
"custom_attributes": [
"id": 2310,
"name": "isReady",
"value": true,
"type": "CustomizableAttribute::Boolean"
,
"id": 2323,
"name": "othernames",
"value": "something",
"type": "CustomizableAttribute::Text"
]
]
`
Code:
public class HivebriteUserList
public List<HivebriteUserRecords> users;
public class HivebriteUserRecords
public String name;
public String email;
public List<HivebriteUserCustomAtt> custom_attributes;
public List<Integer> sub_network_ids;
public class HivebriteUserCustomAtt
public String id;
public String name;
public String value;
apex json callout parser
apex json callout parser
New contributor
New contributor
edited 4 hours ago
Pranay Jaiswal
9,59231949
9,59231949
New contributor
asked 4 hours ago
Anthony Mealand
61
61
New contributor
New contributor
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
For this situation, I would use JSON.desertializeUntyped
which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken
stuff).
Your job is then to check what is in the maps -get
using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser
class.
If the variation is only by the type of a field, then you can store the value in an Object
type property in your data structure:
public class HivebriteUserCustomAtt
public String id;
public String name;
public Object value;
and handle the various types elsewhere.
Or if you want to look at the types straight after parsing use e.g.:
Object value = m.get('value');
if (value instanceOf String)
String s = (String) value;
...
else if (value instanceOf Boolean)
Boolean b = (Boolean) value;
...
...
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
For this situation, I would use JSON.desertializeUntyped
which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken
stuff).
Your job is then to check what is in the maps -get
using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser
class.
If the variation is only by the type of a field, then you can store the value in an Object
type property in your data structure:
public class HivebriteUserCustomAtt
public String id;
public String name;
public Object value;
and handle the various types elsewhere.
Or if you want to look at the types straight after parsing use e.g.:
Object value = m.get('value');
if (value instanceOf String)
String s = (String) value;
...
else if (value instanceOf Boolean)
Boolean b = (Boolean) value;
...
...
add a comment |Â
up vote
3
down vote
For this situation, I would use JSON.desertializeUntyped
which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken
stuff).
Your job is then to check what is in the maps -get
using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser
class.
If the variation is only by the type of a field, then you can store the value in an Object
type property in your data structure:
public class HivebriteUserCustomAtt
public String id;
public String name;
public Object value;
and handle the various types elsewhere.
Or if you want to look at the types straight after parsing use e.g.:
Object value = m.get('value');
if (value instanceOf String)
String s = (String) value;
...
else if (value instanceOf Boolean)
Boolean b = (Boolean) value;
...
...
add a comment |Â
up vote
3
down vote
up vote
3
down vote
For this situation, I would use JSON.desertializeUntyped
which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken
stuff).
Your job is then to check what is in the maps -get
using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser
class.
If the variation is only by the type of a field, then you can store the value in an Object
type property in your data structure:
public class HivebriteUserCustomAtt
public String id;
public String name;
public Object value;
and handle the various types elsewhere.
Or if you want to look at the types straight after parsing use e.g.:
Object value = m.get('value');
if (value instanceOf String)
String s = (String) value;
...
else if (value instanceOf Boolean)
Boolean b = (Boolean) value;
...
...
For this situation, I would use JSON.desertializeUntyped
which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken
stuff).
Your job is then to check what is in the maps -get
using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser
class.
If the variation is only by the type of a field, then you can store the value in an Object
type property in your data structure:
public class HivebriteUserCustomAtt
public String id;
public String name;
public Object value;
and handle the various types elsewhere.
Or if you want to look at the types straight after parsing use e.g.:
Object value = m.get('value');
if (value instanceOf String)
String s = (String) value;
...
else if (value instanceOf Boolean)
Boolean b = (Boolean) value;
...
...
edited 4 hours ago
answered 4 hours ago
Keith C
92k1087194
92k1087194
add a comment |Â
add a comment |Â
Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.
Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.
Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.
Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237155%2fhow-to-read-json-that-contains-field-that-can-have-different-types%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password