{"id":3354,"date":"2022-10-21T12:14:09","date_gmt":"2022-10-21T12:14:09","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3354"},"modified":"2023-01-25T10:17:24","modified_gmt":"2023-01-25T10:17:24","slug":"get_property","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3354","title":{"rendered":"Get_property"},"content":{"rendered":"\n<p>This function can get a value of a data key.<\/p>\n\n\n\n<p>Normally, the single dot &#8220;.&#8221; is used to access the values of objects (i.e. $object.$key). While this function can achieve the same result, it can also be used to get properties with variable names, since it accepts a string value.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">any get_property(json_object $object, string $key)<\/code><\/pre>\n\n\n\n<p>Outputs the value of defined $object.$key.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$object:<\/strong> The object holding the property to get. Must be a JSON object.<br><strong>$key:<\/strong> The key in the defined $object. Must be a string value.<\/p>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"data\": {\n        \"address1\": \"147 Columbia St\",\n        \"address2\": \"541 Livingston St\",\n        \"departments\": [\n            {\n                \"name\": \"Sales East\",\n                \"location\": \"address1\"\n            },\n            {\n                \"name\": \"Sales West\",\n                \"location\": \"address2\"\n            }\n        ]\n    }\n}<\/code><\/pre>\n\n\n\n<p>In this case the data has internal references. The &#8220;departments&#8221; reference other data points. To get the referenced values, get_property must be used.<\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    \/\/These values represent the similarity between the dot and get_property\n    sameResultDot: data.address1,\n    sameResultGetProperty: get_property(data, address1),\n\n    \/\/These values represent the difference between the dot and get_property\n    dotAccessorValue: data.departments[*].{ name: name,  location: location },\n    getPropertyValue: data.departments[*].{ name: name,  location: get_property(parent(@), location) }\n}<\/code><\/pre>\n\n\n\n<p>In this transformation, both the dot-accessor and the get_property functionality is used to illustrate the different results.<\/p>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"sameResultDot\": \"147 Columbia St\",\n    \"sameResultGetProperty\": \"147 Columbia St\",\n    \"dotAccessorValue\": [\n        {\n            \"name\": \"Sales East\",\n            \"location\": \"address1\"\n        },\n        {\n            \"name\": \"Sales West\",\n            \"location\": \"address2\"\n        }\n    ],\n    \"getPropertyValue\": [\n        {\n            \"name\": \"Sales East\",\n            \"location\": \"147 Columbia St\"\n        },\n        {\n            \"name\": \"Sales West\",\n            \"location\": \"541 Livingston St\"\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>The &#8220;location&#8221; keys in the &#8220;dotAccessorValue&#8221; array are incorrect, as they display the reference key names, and not the <em>value <\/em>of the reference keys. <\/p>\n\n\n\n<p>The &#8220;location&#8221; keys in the &#8220;getPropertyValue&#8221; array are correctly displaying the values of the reference keys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This function can get a value of a data key. Normally, the single dot &#8220;.&#8221; is used to access the values of objects (i.e. $object.$key). While this function can achieve the same result, it can also be used to get properties with variable names, since it accepts a string value. Syntax Outputs the value of [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"parent":271,"menu_order":60,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3354"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3354"}],"version-history":[{"count":20,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3354\/revisions"}],"predecessor-version":[{"id":3835,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3354\/revisions\/3835"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}