cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

hasOwnProperty() and fields containing special characters

tk42
New Contributor III

Given the object:

    { 
        "Errors" : {
            "$.Field_1" : "Too many characters.",
            "Field_2" : "Too few characters."
        }
    }

The following expressions yield different results:

$Errors.hasOwnProperty('$.Field_1') ==> false
$Errors.hasOwnProperty('Field_2') ==> true

We tried all manner of escaping the โ€œ$.Field_1โ€ to no avail, before finding a different path that does not require the โ€œhasOwnPropertyโ€ call.

But I am still curious if this is an issue of not escaping the argument correctly, or a bug in the hasOwnProperty() implementation?

Thanks,
TK

4 REPLIES 4

tstack
Former Employee

Sorry, looks like an ancient bug in hasOwnProperty(). Itโ€™s treating its argument as a JSON-Path instead of a plain property name. Thatโ€™s wrong. Not sure how to workaround it yet.

del
Contributor III

Getting a little creative, I got this to work for me:

JSON.stringify($Errors).contains(โ€˜โ€œ$.Field_1โ€:โ€™)

$Errors.hasOwnProperty(โ€œ[โ€˜$.field1โ€™]โ€) gives the correct answer.

Thanks & Regards,
Naveen

The bug will eventually be fixed, so I would prefer people not try to workaround it like this since it will begin failing after the fix.