{"id":2217,"date":"2022-05-04T15:23:02","date_gmt":"2022-05-04T15:23:02","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=2217"},"modified":"2023-05-01T12:39:00","modified_gmt":"2023-05-01T12:39:00","slug":"what-are-key-value-pairs","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=2217","title":{"rendered":"Key\/Value Pairs"},"content":{"rendered":"\n<p>When using JMESPath for transforming JSON, a basic concept that is used a lot, is the notion of a key\/value pair. We use key\/value pairs to order our data in both JMESPath and JSON. <\/p>\n\n\n\n<p>Let&#8217;s take a look at a simple example of how it looks in JSON.<\/p>\n\n\n\n<pre title=\" Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"name\": \"John Doe\"\n}<\/code><\/pre>\n\n\n\n<p>Here we have two strings being separated with a <code>:<\/code>. The string on the left side <code>\"name\"<\/code> is the key which, when referenced to it, will return the value string <code>\"John Doe\"<\/code>.  This is a simple example on a key\/value pair in JSON, where the value is a string. It should be noted that the key should always be a string, but the value can be of any allowed <a rel=\"noreferrer noopener\" href=\"https:\/\/www.w3schools.com\/js\/js_json_datatypes.asp\" data-type=\"URL\" data-id=\"https:\/\/www.w3schools.com\/js\/js_json_datatypes.asp\" target=\"_blank\">JSON data type<\/a>. <\/p>\n\n\n\n<p>When transforming the JSON with JMESPath in Documotor, we follow the same structure with the key\/value pair, where we define new keys based on the contents of the JSON data. However, you can also create key\/value pairs in JMESPath with customized data. Let&#8217;s look at an example with both JSON and JMESPath. <\/p>\n\n\n\n<p>Assume we have the following JSON sample data. <\/p>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  \"firstName\": \"John\",\n  \"lastName\": \"Doe\",\n  \"age\": 34\n}<\/code><\/pre>\n\n\n\n<p>With JMESPath, we can define a new key\/value pair. See below how we transform the sample data. <\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  name: join(' ', [firstName, lastName]),\n  age: age\n}<\/code><\/pre>\n\n\n\n<p>Note that we in JMESPath do not have to surround the key with <code>\"<\/code> as we have to do with the JSON key. In the value, instead of writing a string, we use the <code>join<\/code> function to concatenate the two values from the JSON sample data. We also do not have to surround the JSON keys we are referencing to in the function with <code>\"<\/code> either. When defining a JMESPath transformation, it is only the data defined in this transformation that is passed forward. If you look at line 3 of the JMESPath transformation, you can see that we define a key\/pair value that simply repeats the key from the JSON sample data in order to make sure the data is passed on to the result data. In the <code>age: age<\/code> key\/value pair, the value is referring to our JSON sample data. See below the result from the above transformation. <\/p>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  \"name\": \"John Doe\",\n  \"age\": 34\n}<\/code><\/pre>\n\n\n\n<p>To learn more about Key\/Value pairs, you can read about the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.w3schools.com\/js\/js_json_syntax.asp\" data-type=\"URL\" data-id=\"https:\/\/www.w3schools.com\/js\/js_json_syntax.asp\" target=\"_blank\">JSON Syntax<\/a> for further elaboration. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using JMESPath for transforming JSON, a basic concept that is used a lot, is the notion of a key\/value pair. We use key\/value pairs to order our data in both JMESPath and JSON. Let&#8217;s take a look at a simple example of how it looks in JSON. Here we have two strings being separated [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"parent":2252,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/2217"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2217"}],"version-history":[{"count":22,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/2217\/revisions"}],"predecessor-version":[{"id":5480,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/2217\/revisions\/5480"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/2252"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}