3

please help me, I need a method for json string which contains null value and does not contain attributes{ }.

Here's what I currently have:

List<Contact> contacts = [SELECT Id, Name, account.name,account.Id,account.ParentId FROM Contact limit 3];
system.debug(json.serialize(contacts));

which returns a jsonString that looks like this:

 "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v31.0/sobjects/Contact/003900000115sPwAAI"
  },
  "Name" : "Rose Gonzalez",
  "AccountId" : "00190000010GvcLAAS",
  "Account" : {
    "attributes" : {
      "type" : "Account",
      "url" : "/services/data/v31.0/sobjects/Account/00190000010GvcLAAS"
    },
    "Name" : "Edge Communications",
    "Id" : "00190000010GvcLAAS"
  },
  "Id" : "003900000115sPwAAI"
}, {
  "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v31.0/sobjects/Contact/003900000115sPxAAI"
  },
  "Name" : "Sean Forbes",
  "AccountId" : "00190000010GvcLAAS",
  "Account" : {
    "attributes" : {
      "type" : "Account",
      "url" : "/services/data/v31.0/sobjects/Account/00190000010GvcLAAS"
    },
    "Name" : "Edge Communications",
    "Id" : "00190000010GvcLAAS"
  },
  "Id" : "003900000115sPxAAI"
}, {
  "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v31.0/sobjects/Contact/003900000115sPyAAI"
  },
  "Name" : "Jack Rogers",
  "AccountId" : "00190000010GvcMAAS",
  "Account" : {
    "attributes" : {
      "type" : "Account",
      "url" : "/services/data/v31.0/sobjects/Account/00190000010GvcMAAS"
    },
    "Name" : "Burlington Textiles Corp of America",
    "Id" : "00190000010GvcMAAS"
  },
  "Id" : "003900000115sPyAAI"
} ]

What I need is help putting what I've written above into something that will return a string in a format that looks like what's below:

 {
  "Name" : "Rose Gonzalez",
  "AccountId" : "00190000010GvcLAAS",
  "Account" : {    
    "Name" : "Edge Communications",
    "Id" : "00190000010GvcLAAS",
   "ParentId":null

  },
  "Id" : "003900000115sPwAAI"
}, {

  "Name" : "Sean Forbes",
  "AccountId" : "00190000010GvcLAAS",
  "Account" : {

    "Name" : "Edge Communications",
    "Id" : "00190000010GvcLAAS",
    "ParentId" :null
  },
  "Id" : "003900000115sPxAAI"
}, {

  "Name" : "Jack Rogers",
  "AccountId" : "00190000010GvcMAAS",
  "Account" : {

    "Name" : "Burlington Textiles Corp of America",
    "Id" : "00190000010GvcMAAS",
    "ParentId" :null
  },
  "Id" : "003900000115sPyAAI"
} ]
  • 2
    can you provide more details on what you need ? like what's your current json string structure is , where you are trying to do this , your existing code if possible .. – Vamsi Krishna Aug 4 '14 at 10:57
1

Here is a post that explains the reason:

JSON.Serialize method not returning null fields

The fix, version your Apex before Spring 13' I believe it is v 25 or like in the post v27 post fix. I believe since then they have again instituted the change to not serialize nulls

Or you will have to write your own serializer using JSONGenerator methods and the writeNull() functions:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_System_JsonGenerator.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.