28

I suggest you paste your JSON into http://json2apex.herokuapp.com/ and try the generated code. This tool generates simple Apex classes with a field per JSON field and then you can parse with a single JSON.deserialize call. Here is the code produced from your JSON. (You can rename the classes as you see fit and also change data types if you know better e.g. ...


21

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 compilation of the code. Type casting declares for the Apex compiler what type of value you expect to get back from a generic function, like JSON....


13

Declare your member transient and the JSON serializer will ignore it. A quick validation in an anonymous block (yes, you can declare classes inside anonymous blocks!) public enum Obscurity{ OBVIOUS, OPAQUE, WTF } public class Thing{ public Obscurity obscurityLevel; public transient Obscurity notSerialized; } Thing t = new Thing(); t.obscurityLevel =...


13

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": "0036100000JUXKkAAP","Description": "","LastName": "Testing"}}'; Map<String, Object> cObjMap = (Map<String, Object>) JSON.deserializeUntyped(jsonText); ...


13

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 could break things. In other words, you can't reliably use String.valueOf unless you can also use it in System.debug and get the output you expect. String....


12

Take a look at JSON.deserialize(); (documentation) In your case, what you will have to do is create 2 inner classes like so: public class CompanyContacts { public List<CompanyContactsWrapper> CompanyContacts; } public class CompanyContactsWrapper { public String contact; public String postalcode; public String contactnumber; } ...


12

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 custom object, it is flat out incorrect. What they mean is an instance of a custom defined class. Let's check: Script class Demo { } Demo instance = (Demo)JSON....


11

You probably need to query more fields to expose them in your serialization. It would help if you posted your getRecord() code, but the following ought to work: SObject getRecord() { List<String> fieldsToQuery = Proposal__c.SObjectType.getDescribe().fields.getMap().values(); String fields = String.join(fieldsToQuery, ', '); String ...


11

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 with [ and ends with ]. Those delimiters are used to indicate a List (or Array if you prefer) in JSON. I think you need to deserialize as below: String str = ...


11

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 created reasonable easily: List<Map<String, Object>> results = new List<Map<String, Object>>(); for (AggregateResult ar : [SELECT Name ...


10

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 Attributes visible to VisualForce At the end of this post, you will find the class with different types of attributes (public; getter; getMethod; auraEnabled; ...


10

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 efficient. public class LineModel { String a; String b; String c; MyAccount record; } public Class MyAccount{ ...


9

The easiest solution (and most efficient in terms of script statements!) is going to be for you to head to Json2Apex (written by metadaddy and superfell), plug your JSON in, and away you go! That will spit out this class: // // Generated by JSON2Apex http://json2apex.herokuapp.com/ // public class CompanyContactsParser { public class CompanyContacts { ...


9

In JavaScript/JSON null is defined but NULL isn't. Try null. (Which is different from "null" or 'null' which are strings.) I just checked and Apex's JSON.deserializeUntyped throws System.JSONException: Unrecognized token 'NULL' for NULL but works with null i.e. it is (not to surprisingly) consistent with JavaScript/JSON.


8

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 getSomeProperty() { return typeName; } public abstract List<User> doStuff(); } Granted, this definition cannot be constructed either, but ...


8

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; } // If you are sure data is null or numerical but never string("N/A") ... } Then cast as this: Map<String, Subdata> meta = (Map<String, Subdata>) ...


8

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 languages too. The solution here is to store the configuration for your callout in something like a Map<String, String> or a custom configuration wrapper ...


7

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 this structure: [ {"replacement": "...", "name": "...", ...}, {...}, {...} ] A JSON array (the [ ]) is converted to an Apex list and a JSON object (...


6

The problem is that you're casting to a list when the object is a map. You want a list of maps. This should do it: List<Map<String,Object>> return_posts = new List<Map<String,Object>>(); for (String p : posts.keySet()) { Map<String,Object> pp = (Map<String,Object>)posts.get(p); return_posts.add(pp); }


6

You can Deserialize the Json Array to a list of accounts like this List<Account> accountsDeserialized = (List<Account>) JSON.deserializeStrict(jSONRequestBody, List<Account>.class); Please find the below example tried with a JSON Array. I used Workbench for testing. Apex class @RestResource(urlMapping='/v1/accounts/*') global with ...


6

You will need to cast the results to the type you know them to be so that the compiler knows that there is a get method: Map<String, Object> results = (Map<String, Object>) m.get('results'); Repeat the casting as you go down through the layers.


6

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. There's likely a way to abstract this pattern, but the basic idea that I used was to have an outer class to start the parsing, and then have a bunch of inner ...


6

Just deserialize it as a Map<String, Object>: String response = '{"access_token":"...","other_properties":"..."}'; Map<String, Object> resMap = (Map<String, Object>)JSON.deserializeUntyped(response); String accessToken = (String)resMap.get('access_token'); System.debug('Access Token : '+accessToken);


6

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 as you have mentioned: Variable does not exist: User.class Solution: Delete or rename User custom class and try again. Better Solution mentioned by ...


6

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; public Map<String, Locale> languages; } public class Locale { public String langCountry; public String defaultLang; } That saves you from ...


6

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 only include letters, numbers, and underscores, can't have two consecutive underscores, and can't be certain reserved keywords. JSON.deserialize does not ...


5

If you didn't serialize the object into JSON, then the object would not be in a format that the consumer (the system who did the request) would understand. The purpose of serializing it into JSON is so that the message will be a format that can be understood and from there, deserialize it into an object type that makes sense for the consumer.


5

The DateTime comes back ISO8601 formatted. So depending on your language of choice it should be fairly simple to parse them and then reformat as required. E.g. .NET DateTime.Parse("2013-11-27T20:58:00.000+0000"); E.g. Apex DateTime d = (DateTime)JSON.deserialize('2013-11-27T20:58:00.000+0000', DateTime.class);


5

It would help to see the proxy generated by WSDL->Apex, the response from the WS, or just the schema. From the error message, you get a response from the WS, but de-serialization fails. First, you could call the WS from SOAP UI to get a proper image of what you are getting back. Second, check if you have any numeric properties in the Proxy. Apparently, ...


5

Well, you do not want to do DML in a loop. Thats an apex anti-pattern and will quickly run you afoul of governor limits. What you need to do instead, is use a for loop to iterate over the collection of Entities building sObjects from them, and adding them to a list. then, with one dml call, insert that entire list. here's some psuedo code: List<...


Only top voted, non community-wiki answers of a minimum length are eligible