1

I've got this query:

SELECT name, (SELECT From__c, To__c FROM Trip__r) FROM Account limit 1


[ {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v29.0/sobjects/Account/001b000000HZ"
  },
  "Name" : "Adam II",
  "Trip__r" : {
    "totalSize" : 2,
    "done" : true,
    "records" : [ {
      "attributes" : {
        "type" : "Trip__c",
        "url" : "/services/data/v29.0/sobjects/Trip__c/a00b000000AOj"
      },
      "To__c" : "2013-11-27T15:15:00.000+0000",
      "Id" : "a00b000000AOj",
      "Car__c" : "001b000000HZ",
      "From__c" : "2013-11-25T15:15:00.000+0000"
    }, {
      "attributes" : {
        "type" : "Trip__c",
        "url" : "/services/data/v29.0/sobjects/Trip__c/a00b000000A"
      },
      "To__c" : "2013-11-28T20:58:00.000+0000",
      "Id" : "a00b000000AOlR",
      "Car__c" : "001b000000HZp",
      "From__c" : "2013-11-27T20:58:00.000+0000"
    } ]
  },
  "Id" : "001b000000HZp"
} ]

But, I need to get rid this part

   "Trip__r" : {
    "totalSize" : 2,
    "done" : true, 

So it looks like

[ {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v29.0/sobjects/Account/001b000000HZ"
  },
  "Name" : "Adam II",
    "records" : [ {
      "attributes" : {
        "type" : "Trip_c",
        "url" : "/services/data/v29.0/sobjects/Trip_c/a00b000000AOj"
      },
      "To_c" : "2013-11-27T15:15:00.000+0000",
      "Id" : "a00b000000AOj",
      "Car_c" : "001b000000HZ",
      "From_c" : "2013-11-25T15:15:00.000+0000"
    }, {
      "attributes" : {
        "type" : "Trip_c",
        "url" : "/services/data/v29.0/sobjects/Trip_c/a00b000000A"
      },
      "To_c" : "2013-11-28T20:58:00.000+0000",
      "Id" : "a00b000000AOlR",
      "Car_c" : "001b000000HZp",
      "From_c" : "2013-11-27T20:58:00.000+0000"
    } ],
  "Id" : "001b000000HZp"
} ]

I tried to look around wrappers, deserialisations, but still can't understand them! Any help would be very appreciated!

7
  • 1
    Try json2apex.herokuapp.com. It'll generate Apex that parses the JSON into a class.
    – Mike Chale
    Commented Nov 25, 2013 at 22:06
  • I seriously don't get the purpose of that app. It puts everything into one great string - so what? For my testing I've put the string into VF page and parsed with JS, just to see where the snags were. Now I need to adjust the JSON output a bit.
    – dzh
    Commented Nov 25, 2013 at 22:12
  • j2a builds Apex Code that you can copy-paste into your org to serialize/deserialize the JSON strings (it's not perfect, though, so you may need to tweak the results). It also includes a test method (that was the great JSON string you saw).
    – sfdcfox
    Commented Nov 25, 2013 at 22:40
  • 1
    I kinda forgot to mention that I am serialising my SOQL output. List<Account> accounts = [SELECT name, (SELECT From_c, To_c FROM Trip__r) FROM Account limit 1]; return JSON.serializePretty(accounts)
    – dzh
    Commented Nov 25, 2013 at 23:03
  • 1
    @MikeChale seems like the issue is really that subqueries don't serialize well, since under the hood they're a query locator rather than a full result set. Commented Nov 25, 2013 at 23:31

1 Answer 1

1

trip__c extends completely like :

"Trip__r" : {
    "totalSize" : 2,
    "done" : true,
    "records" : [ {
      "attributes" : {
        "type" : "Trip__c",
        "url" : "/services/data/v29.0/sobjects/Trip__c/a00b000000AOj"
      },
      "To__c" : "2013-11-27T15:15:00.000+0000",
      "Id" : "a00b000000AOj",
      "Car__c" : "001b000000HZ",
      "From__c" : "2013-11-25T15:15:00.000+0000"
    }

you need to completely remove this part.

1> convert this list to JSON string using

String str=JSON.stringify(list);

2>perform string manipulation on this string to remove unnecessry part.
3>convert the string back to list object using JSON.desrialize()
ex :

list<account> acclist=(list<Account>)JSON.deserialize(str,list<Account>.class);

and one more thing that can be done is, you can look at documentation for JSON and JSONParser class that might be helpful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .