Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this Page

...

hasOwnProperty

Description

Indicates whether Returns a boolean value to indicate whether the object has the specified property.

The in operator and get method can be used as a shorthand to test if an object has a property or get the value of a property with a default if it does not exist.

Note

This argument is treated as a JSONPath instead of a plain property name and returns false if the property exists but the value is null. Most expressions should work as expected if the object is not a qualified path. If your expression passes a JSONPath or if the property value is null, use the hasPath method instead.

For example, if we use this object method to look up a property in an object, then we would have the following expression:

$.map.hasOwnProperty("route")

Where the property "route" is the target property in the object $.map.

Using this object method to find a path does not work as might be expected. For example:

$.hasOwnProperty("map.route")

Where map.route is a path and not a property.

In this case, we recommend you use the hasPath() method.


Syntax


Code Block
object.hasOwnProperty(field)


Example

Expression: $.hasOwnProperty("Idroute") where $ object corresponds to my.custom.route

Result: Returns true if the object has the key "Id".


Expression: To create a ternary conditional expression:$.hasOwnProperty('query"custom.route") where $ object corresponds to my.custom.route

Result:

  • Returns false if the value of route is null as custom.route is not a property but a path. (Reason to use hasPath() function instead.)
  • Returns true if the value of route is not null though custom.route is not a property but a path.


Expression: To create a ternary conditional expression:

$.hasOwnProperty('query') ? $query : 'not present in input'

hasPath


Example
DescriptionIndicates whether

Returns a boolean value to indicate whether the object has the specified property (path) that carries a non-null value. This method is recommended when working with JSONPath, especially when looking for fields nested deep within the object.

Info

Instead of chaining multiple sub-expressions using the hasOwnProperty method to validate the subpath at each node of the JSONPath, you can call hasPath only once to do the same validation. You must provide the JSONPath to the property.


Syntax


Paste code macro
object.hasPath(JSONPath.to.property)


Example

Expression: $.hasPath("route") where $ object corresponds to my.custom.

property)

route

Result: Returns true.


Expression:  $.hasPath("Idcustom.route") where $ object corresponds to my.custom.route

Result:

  • Returns false if the value of route is null.
  • Returns true
 if the object has the key "Id"
  • if the value of route is not null.


Example JSON Object:

Paste code macro
languagejson
titleJSON Example
{
  "Name":"previewdata.json",
  "Type":"File",
  "Size (in bytes)":"4",
  "Owner":null,
  "Update date":"2019-10-06T16:50:58.000Z",
  "Editor": {
    "EditorName": "John Doe",
    "Dept": "Marketing"
  }
  "Creation date":"unknown",
  "Permissions":"unknown"
}

Expression$.hasPath("Type")

Result: Returns True true because the object "Type" has a specified value. 

Expression$.hasPath($.Editor.EditorName)

Result: Returns True true because "EditorName" has a specified value. 

Expression: $.hasPath("Owner")

Result: Returns False false since even though the object has the key "Owner", it is null.

Expression: $.hasPath("Region")

Result: Returns False false since the object does not have "Region".

isEmpty

...

Expand
titleExtend Method: Converting an array into an object

A JSON Array of objects with unique/non-overlapping keys can be converted to an object using the extend object method along with a Spread Operator.  The extend object method does so by creating objects dynamically from other objects passed in as arguments, the spread operator will have to be used to indicate that the elements of the array should be treated as the arguments to the function. The expression to be used is:


Paste code macro
titleExpression using extend method to convert an array into an object
{}extend(...$<Array_name>)

1. Input the array into Mapper Snap (this can be done using a JSON Generator Snap). For this example, the following array will be used:

Paste code macro
languagejson
titleJSON Array
{
 "myArray": [
   {
     "Header1": {
       "a": 11,
       "b": 21,
       "c": 31
     }
   },
   {
     "Header2": {
       "a": 12,
       "b": 22,
       "c": 32
     }
   },
   {
     "Header3": {
       "a": 13,
       "b": 23,
       "c": 33
     }
   }
 ]
}

2. The Mapper Snap must be configured as shown below. Notice the Expression and Target path fields.

3. When executed, the output will be an object.

Note

The array should have unique keys for this method to work, if they overlap then the last element of the array will be converted.