Versions Compared

Key

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

In this Page

...

Description

Find the value for the given property name given. If the property does not exist, the function returns the default value (if given); otherwise, null is returned. If the value is a populated list, then the function returns the first value in the list; otherwise, it returns the defaultValue default value if present or null.

Syntax


object.getFirst(propertyName, defaultValue)


Example

Expression$.getFirst("test")

Where $test  is the string "abc123"

Result: abc123


Expression$.getFirst("test")

Where $test is a list consisting of [5, 10, 15, 20]

Result: 5

...

Description

Indicates 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 function is not supported in Spark pipelines.


Note

The function does not currently match the JavaScript behavior and will be changed in the near future. In particular, the argument is treated as a JSON-Path instead of a plain property name and it will return false if the property exists, but the value is null. Most expressions should continue to work as expected since they are not qualified paths. If your expression passes a JSON-Path or if the property value is null, use the hasPath method instead. 


Syntax


Code Block
object.hasOwnProperty(field)


Example

Expression: $.hasOwnProperty("Id")

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


Expression: To create a ternary conditional expression:

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

...

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.