[SalesForce] JSON from nested query

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!

Best Answer

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.