question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How To Read Custom Data From Catalog Item?

Hello everyone,

I am trying to read custom data from a catalog item in the cloud script.

Here's the code. It returns "Top Speed is null" message; however, when I debug the custom data, the data is not null. My question is how can I access the custom data?

    if(rivalCar.CustomData == null) {
        return { "Result": "Custom data null" };
    }
    else if(rivalCar.CustomData["TopSpeed"] == null) {
        return { "Result": "Top Speed is null" };
    }
    else {
        var r = rivalCar.CustomData["TopSpeed"];
        return { "Result": r };
    }
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

Here's what custom data includes

{"CustomData":"{\"TopSpeed\":\"151\",\"Acceleration\":\"13.7\",\"Handling\":\"630\"}"}
0 Likes 0 ·

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Because the “CustomData” is string currently, you cannot get the value of “Top Speed” via “rivalCar.CustomData["TopSpeed"]”. If you want to handle the “CustomData” as an object, you need to convert it to object using the following code.

if (rivalCar.CustomData == null) {
    return {
        "Result": "Custom data null"
    };
} else {
    // Parse the JSON string to object.
    var CustomDataObj = JSON.parse(rivalCar.CustomData);
    if (CustomDataObj.TopSpeed == null) {
        return {
            "Result": "Top Speed is null"
        };
    }
    var r = CustomDataObj.TopSpeed;
    return {
        "Result": r
    };
}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.