{"id":3900,"date":"2023-03-22T12:28:31","date_gmt":"2023-03-22T12:28:31","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3900"},"modified":"2023-03-22T12:28:31","modified_gmt":"2023-03-22T12:28:31","slug":"to_dictionary","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3900","title":{"rendered":"To_dictionary"},"content":{"rendered":"\n<p>To_dictionary can be used to convert an array into multiple objects where each object takes the value of a defined key.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">json_object to_dictionary(array $input, expr &amp;key, expr &amp;value)<\/code><\/pre>\n\n\n\n<p>Groups all objects within the <code>$input<\/code> array by a defined common <code>&amp;key<\/code>. It converts the input array into a single object which in turn contains an object for each unique <code>&amp;key<\/code> and applies an expression defined by <code>&amp;value<\/code> to each of these objects.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$input:<\/strong>&nbsp;The array to evaluate.<br><strong>$key:<\/strong>&nbsp;The items within each array to group by.<br><strong>$value:<\/strong> The expression to apply to object.<\/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  \"SalesData\": [\n    {\n      \"Region\": \"Germany\",\n      \"Product\": \"Apple\",\n      \"Units\": \"51\"\n    },\n    {\n      \"Region\": \"Denmark\",\n      \"Product\": \"Apple\",\n      \"Units\": \"99\"\n    },\n    {\n      \"Region\": \"Germany\",\n      \"Product\": \"Orange\",\n      \"Units\": \"66\"\n    },\n    {\n      \"Product\": \"Apple\",\n      \"Units\": \"51\"\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n  SalesDataByRegion: to_dictionary(SalesData, &amp;Region, &amp;@.Product)\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  \"SalesDataByRegion\": {\n    \"Germany\": [\n      \"Apple\",\n      \"Orange\"\n    ],\n    \"Denmark\": [\n      \"Apple\"\n    ],\n    \"_\": [\n      \"Apple\"\n    ]\n  }\n}<\/code><\/pre>\n\n\n\n<p>This example shows <code>to_dictionary<\/code> being used to create a list of each product sold in a region where both the product name and region names exist as key\/value pairs within each object. Two of these objects have a region with value Germany, one has a region with value Denmark and one has no region value. The <code>$value<\/code> expression then uses the current node scope operator to extract ONLY the <code>Product<\/code> from each object. The result is a list of each product per region.<\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p>Where the defined key does not exist, objects will be grouped together under a default key, &#8220;_&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To_dictionary can be used to convert an array into multiple objects where each object takes the value of a defined key. Syntax Groups all objects within the $input array by a defined common &amp;key. It converts the input array into a single object which in turn contains an object for each unique &amp;key and applies [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"parent":271,"menu_order":193,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3900"}],"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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3900"}],"version-history":[{"count":8,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3900\/revisions"}],"predecessor-version":[{"id":5356,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3900\/revisions\/5356"}],"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=3900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}