Forum Discussion

tk42's avatar
tk42
New Contributor III
9 years ago

hasOwnProperty() and fields containing special characters

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

  • tstack's avatar
    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's avatar
    del
    Contributor III

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

    JSON.stringify($Errors).contains(‘“$.Field_1”:’)

    • nganapathiraju's avatar
      nganapathiraju
      Former Employee

      $Errors.hasOwnProperty(“[‘$.field1’]”) gives the correct answer.

      Thanks & Regards,
      Naveen

      • tstack's avatar
        tstack
        Former Employee

        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.