21
votes
Accepted
Unfamiliar Syntax
This is called a type cast. Apex has what is called a strict, static typing discipline, which means that all variables and values have a type, and that type is declared for variables at the time of ...
16
votes
Accepted
Deserialize JSON to SObject
Well, this is how you can do it. Apex is made through Java!
String jsonText = '{"Data": {"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Id": "...
13
votes
Accepted
Why is String.valueOf(Blob) different that Blob.toString()?
String.valueOf returns Blob[X] for Blob values. I believe it does it this way because System.debug internally uses String.valueOf, and debugging a potentially non-Unicode stream into a Unicode String ...
12
votes
Accepted
deserializeStrict method: bug?
Aside from that last sentence the documentation is spot on. However, I would argue that the behavior is correct, and it is just that sentence which is flawed.
It is easy to demonstrate that for a ...
12
votes
Accepted
How to Deserialize AggregateResults?
I would have expected the serialization to work...
A workaround is to use Map<String, Object> instead (as an AggregateResult is usually consumed much like a Map) which will serialize and can be ...
11
votes
Json Deserialization
Invalid conversion from runtime type List to Map
So it is expecting a List which you are typecasting to a Map that's the issue. The thing that makes it a List is the fact that the JSON String starts ...
11
votes
Accepted
Aura vs Apex - Deserialization and Interface discrepancy (Major rewrite with git repo)
There are four different things that work different and should not be mixed up:
Attributes visible to Apex (System.debug(object))
Attributes visible to JSON.serialize
Attributes visible to Aura
...
10
votes
Accepted
Deserialize hits memory limit
In your wrapper class LineModel, don't use Standard Account. If you do that you will also deserialize children. Instead, create Apex Wrapper for Account with fields you need. This will be more memory ...
10
votes
Accepted
How to loop trough second level deserializeUntyped Json String
The result from JSON.deserializeUntyped is either a List, Map, String, Boolean, or Decimal/Integer. You can detect these conditions dynamically, even recursively if you want to.
Object source = JSON....
9
votes
Accepted
JSON Serialize and Deserialize on HttpRequest
There are certain classes that are not serializable, and HttpRequest is one of them. I can't seem to find supporting documentation, but from what I recall this is a pretty common constraint in other ...
8
votes
Accepted
How to deserialize into interface type when concrete type is unknown
I think you should revisit the idea of using an abstract class rather than an interface. You can use the former to enforce a contract.
public class Demo
{
final String typeName;
public String ...
8
votes
Accepted
deserialising JSON best practice/efficiently
You should define a data structure class for the sub-object
public class Subdata {
public String high { get; set; }
public String low { get; set; }
public Decimal percentChange { get; set; } // ...
8
votes
Accepted
Is JSON deserialization in Apex case sensitive?
This doesn't matter when you're doing typed deserialization, but it will be an issue if you do untyped deserialization because any Map<String, Object> that you pull out will necessarily have ...
7
votes
Unfamiliar Syntax
To add to David's answer, the casts in this case are following the pattern of the expected JSON structure and how JSON.deserializeUntyped converts from JSON to Apex types. It looks like the JSON has ...
7
votes
Accepted
Deserializing a list of jsons
Apex's type system is weird. Perhaps someone wiser than me can explicate the underlying principle or limiting case here in terms of the language grammar in more detail, but I will only try to sketch ...
6
votes
How to parse XML data to JSON? How to deserialize XML to Apex class?
There is no quick and easy way to transform XML either into JSON or into an Apex Class.
I came up against this in a recent project, and I ended up using the Dom.Document and Dom.XmlNode classes.
...
6
votes
Accepted
System.TypeException: Invalid conversion from runtime type Map<String,ANY> to Map<String,String>
Just deserialize it as a Map<String, Object>:
String response = '{"access_token":"...","other_properties":"..."}';
Map<String, Object> resMap = (Map<String, Object>)JSON....
6
votes
Accepted
System.JSONException: For input string
public Integer learningRate; //0
In your JSON, it appears as a Decimal value. Change the code as follows:
public Decimal learningRate; //0.0
6
votes
Accepted
JSON.deserialize() for User records wont work
I was able to reproduce the issue in my DEV org by creating a class named User.
Another class has following code:
User user = (User) JSON.deserialize('{}', User.class);
Which yeilds the same error ...
6
votes
Accepted
How to parse nested objects in JSON response
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
...
6
votes
Accepted
Error deserializing json array nullpointer exception
The [] in your JSON won't work. The parser would try to map this to an apex variable named pay_names[]. This is not a valid Apex identifier. Valid identifiers in Apex must start with a letter, can ...
6
votes
Accepted
Declaration of Data Types Apex
The (Map<String, Object>) bit is called type casting. It forces something of one type to be treated as if it were a different type.
In the case of JSON.deserializeUntyped(), the method returns a ...
5
votes
Deserialize JSON to SObject
Things must have changed signficantly in the past few years, because it's quite simple now from what I can see as of Winter '20. It's a 1 line task to deserialize. In this example, I'm deserializing ...
5
votes
Accepted
Deserializing an Account throws an error
It's because the query returns a List<Account> unless you specify it somewhere as a single Account record.
Works
Account data = [SELECT Id, Name FROM Account LIMIT 1];
system.debug(JSON....
5
votes
Accepted
How to parse YAML object in Salesforce Apex
No, at time of writing (API v41.0), Salesforce does not have any built-in functionality to create or parse YAML. That means you'd need to build your own (or adapt an existing one from another language,...
5
votes
JSON Deserialize
As Raul suggested, first replace the Services. from your JSON then create the wrapper class (You can use Json2Apex) and the class will be much simpler as below.
public class ServiceReportWrapper {
...
5
votes
Accepted
How can I deserialize JSON with a (.) in the variable name?
The three basic fixes to any invalid identifier in your JSON keys are:
Deserialize untyped (Map<String, Object> in this case)
Map<String, Object> data = (Map<String, Object>)JSON....
5
votes
Accepted
JSON.deserialize return null values
Just remove ContactJs, You don't need to include class name.
This should work
String jso = '{ "another_Id": "cjfv5g33k61ls7jdlf4353kjg", "first_name": "2", "last_name": " 2 ", "first_name_english": ...
5
votes
Is it possible to convert Map<Object, List<Object>> to Map<Id, List<SomeConcreteSObject>> without a loop?
No. You can't convert key types without a loop. The Map structure is not flexible in this way. The List type is a bit more flexible, however, and so you can make this minor simplification, as ...
5
votes
Accepted
Invalid Conversion Runtime Type List<> to Map<>
The structure of your string payload does in fact clearly indicate a List structure, as it starts with a square bracket ([). You have a List<Map<String, Object>>. However, you have to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
deserialize × 215json × 175
apex × 154
rest-api × 17
map × 17
serialization × 13
callout × 11
rest × 10
parser × 10
serialize × 10
api × 9
list × 8
wrapper-class × 8
visualforce × 6
webservices × 6
apexrest × 6
unit-test × 5
datetime × 5
sobject × 5
string × 5
object × 5
httpresponse × 5
parsing × 5
custom-object × 4
integration × 4