Convert JSON to Parse.Object using fromJSON

I’m trying to convert my Parse object that’s in JSON format into a Parse.Object using the Parse.Object.fromJSON() and while it’s working for my highest level object, it’s not working for nested objects.

I am trying to convert my Products which has a nested pointer of Store and ProductCategory.

After running

Parse.Object.fromJSON({
  className: 'Products',
  ...product,
});

My Product is working as expected to be a Parse.Object. However, my nested pointer is not. My ProductCategory when logging my Product looks like this.

{
  ProductCategory: i
  className: "ProductCategories"
  id: "12345678"
  _localId: undefined
  _objCount: 2892
  attributes: Object
}

The attributes is empty and doesn’t have any of the data from when it was in JSON format.

Is there something I’m doing wrong here?

Would you mind to share the product json?

  "_id": "Products",
  "objectId": "string",
  "updatedAt": "date",
  "createdAt": "date",
  "Name": "string",
  "ProductCategory": "*ProductCategories",
  "SalesPrice": "number",
  "Active": "boolean",
  "IsDeleted": "boolean",
  "Store": "*Stores",
  "Brand": "*Brands",
  "_metadata": {
    "indexes": {
      "_id_": {
        "_id": 1
      }
    },
    "class_permissions": {
      "get": {
        "role:ProductsRead": true,
        "role:ProductsWrite": true
      },
      "find": {
        "role:ProductsRead": true,
        "role:ProductsWrite": true
      },
      "create": {
        "role:ProductsWrite": true
      },
      "update": {
        "role:ProductsWrite": true
      },
      "delete": {
        "role:ProductsWrite": true
      },
      "addField": {
        "role:ProductsWrite": true
      },
      "count": {
        "role:Reports": true
      }
    }
  },
}

That’s the schema and not the json right? I guess that ProductCategory is in the wrong format. If you share the current JSON for it maybe I can guide you. How are you generating this json?

Ahh yeah, my bad, you are right, that was the schema.

I had done a query on products and had included ProductCategory, then on the Product I’d call toJSON().

Here’s the JSON of my product.

{
    "className": "Products",
    "Name": "Test Name",
    "Active": false,
    "IsSample": false,
    "createdAt": "2021-10-18T23:15:05.457Z",
    "updatedAt": "2022-01-04T18:54:49.700Z",
    "objectId": "EI3Cy9aFcg",
    "Quantity": 939,
    "SalesPrice": 500,
    "ProductCategory": {
        "Name": "Test Category",
        "Type": "Test Type",
        "createdAt": "2019-05-31T20:34:19.477Z",
        "updatedAt": "2020-03-04T22:22:53.317Z",
        "ACL": {
            "role:ProductTypesRead": {
                "read": true
            },
            "role:ProductTypesWrite": {
                "read": true,
                "write": true
            }
        },
        "objectId": "blBO4uSJSo"
    },
    "Store": {
        "__type": "Pointer",
        "className": "Stores",
        "objectId": "ZpiYbRa5pC"
    },
    "ACL": {
      "role:ProductsRead": {
          "read": true
      },
      "role:ProductsWrite": {
          "read": true,
          "write": true
      }
  }
}

One thing too, I didn’t include my Store in my product query, so that’s how my store object was populated, but when I call Parse.Object.fromJSON() on the product, that store gets populated and works like a Parse.Object like the product does, but the ProductCategory(s) attributes are empty.

Thanks for the assistance.

Try _toFullJSON() instead of toJSON()

I can’t tell a difference in my products using toJSON versus _toFullJSON. They seem to look the same and when I tried to convert back into a Parse.Object using Parse.Object.fromJSON() I’m still running into the same issue where my ProductCategory is empty.

Have you tried to use the .get() function to check the value of a field? Your object looks mounted correctly now.

I did try using .get() on the `ProductCategory and it returns but when I log it out, the attributes are empty.

When I log my Store in another part of my app, and I expand attributes, I see values which is what I’m not seeing on my ProductCategory that’s nested inside my Product.

image

Can you share again the JSON now that you are using _toFullJSON. ? You should have something like:

"ProductCategory": {
        "__type": "Pointer",
        "className": "ProductCategories",
        "objectId": "blBO4uSJSo",
        "Name": "Test Category",
        "Type": "Test Type",
        "createdAt": "2019-05-31T20:34:19.477Z",
        "updatedAt": "2020-03-04T22:22:53.317Z",
        "ACL": {
            "role:ProductTypesRead": {
                "read": true
            },
            "role:ProductTypesWrite": {
                "read": true,
                "write": true
            }
        }
    },

I just saw on Parse.Query.find() I can pass in { json: true } instead of looping over all my Products and running a _toFullJSON() on them.

This is my ProductCategory which is nested in my Product using the find({ json: true })

{
  className: 'ProductCategories'
  objectId: '1DprHQFAzm0008',
  Store: { __type: 'Pointer', className: 'Stores', objectId: 'ZpiYbRa5pC' },
  Name: 'Test Category',
  Type: 'Test Type',
  createdAt: '2019-03-05T20:08:34.705Z',
  updatedAt: '2019-03-05T20:08:34.705Z',
  ACL: {
    'role:ProductTypesRead': { read: true },
    'role:ProductTypesWrite': { read: true, write: true }
  },
}

When using _toFullJSON() this is what my ProductCategory looks like.

{
   __type: 'Object',
   className: 'ProductCategories'
   Store: { __type: 'Pointer', className: 'Stores', objectId: 'ZpiYbRa5pC' },
   Name: 'Test Category',
   Type: 'Test Type',
   createdAt: '2019-03-05T20:08:34.705Z',
   updatedAt: '2019-03-05T20:08:34.705Z',
   ACL: {
     'role:ProductTypesRead': { read: true },
     'role:ProductTypesWrite': { read: true, write: true }
   },
   objectId: '1DprHQFAzm0008',
 }

Comparing it to what you just posted, I am not getting __type: 'Pointer' for either scenario on my nested ProductCategory within my Product.

Can you set __type: 'Pointer' before using fromJSON() to see if it solves the issue?

My Parse.Object’s don’t look the exact same after using fromJSON() versus when just straight up logging my Parse.Object, but at least I am able to use Product.get('ProductCategory') and also get attributes from the Product Category, Product.get('ProductCategory').get('Name')

This is from logging my ProductCategory directly after querying for it.
image

Here’s my Product after using fromJSON()
image

And the nested pointer of ProductCategory on the Product
image

For some reason there is that _localId: undefined when using the fromJSON() but they are working, so I think I’m good?

What is the _localId for out of curiosity? And why does it get applied when using that fromJSON() versus querying for data that returns Parse.Object format and not JSON format?

Thanks again for the help.

_localId is used for local datastore but I don’t have any idea why it looks different on these cases. I think you are good though.