# Return Plain Objects from Cloud Code

**URL:** <https://community.parseplatform.org/t/return-plain-objects-from-cloud-code/4860>\
**Category:** Cloud Code\
**Created:** [March 5, 2025, 12:26pm UTC](https://community.parseplatform.org/t/return-plain-objects-from-cloud-code/4860 "2025-03-05T12:26:53Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![BobyIlea](https://community.parseplatform.org/user_avatar/community.parseplatform.org/bobyilea/32/1966_2.png) [@BobyIlea](https://community.parseplatform.org/u/BobyIlea)\
**Post date:** [March 7, 2025, 12:01pm UTC](https://community.parseplatform.org/t/return-plain-objects-from-cloud-code/4860/7 "2025-03-07T12:01:53Z")

</div>

I think `query.toJSON` serialises the query itself and not the results of the query. The result is basically an array of `Parse.Object`s if `json:true` is not passed or an array of plain objects if it is.

The problem is that the `map` method only calls the `toJSON` function on the objects in the array and it does not recursively call it on the nested objects. I’d probably need to manually do that, not sure if there is a straight, one-liner JS way of doing it.

With the `query` `json` option it seems that a `__type: 'Object'` field is added to the nested objects and that causes the encoder in `Parse.Cloud.run` function to convert those nested objects into `Parse.Object`s. So this method would work well if you’d have the option to fetch the results directly from a query, but it doesn’t work if you “wrap” that query in a cloud function because of the encoder call from `Parse.Cloud.run`.

A solution like this, where we iterate recursively and just remove the `__type` field instead of recursively calling `toJSON`(like we could do with the `map` solution):

```auto
// In Cloud code
function removeType(obj) {
    for (prop in obj) {
        if (prop === '__type')
            delete obj[prop];
        else if (typeof obj[prop] === 'object')
            removeType(obj[prop]);
    }
}
...
const results = await cardsQuery.find({ json: true });
removeType(results);
return results;

```

Works quite well(and can probably be further optimised), but it’s not pretty and it would be ideal to be able to reach the same result just by using the `json` option in the `Query.find` function.

I’m still not sure if the added `__type` field is a bug or not, since it seems to only appear in the nested objects(eg. included pointers) and does not appear in the top level objects in the results array…

---

_[View the full topic](https://community.parseplatform.org/t/return-plain-objects-from-cloud-code/4860)._
