{"id":12343,"date":"2019-07-15T15:39:38","date_gmt":"2019-07-15T20:39:38","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=12343"},"modified":"2019-09-04T18:20:02","modified_gmt":"2019-09-04T23:20:02","slug":"mule-4-dataweave-2-0-overview","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=12343","title":{"rendered":"Mule 4:Dataweave 2.0 Examples"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Quick_Start\">Quick Start<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Prerequisites\">Prerequisites<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Setup_a_Project\">Setup a Project<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Concatenate_String\">Concatenate String<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#\"><\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#JSON_to_XML\">JSON to XML<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Supported_Data_Types\">Supported Data Types<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Define_and_Use_Var_as_Input\">Define and Use Var as Input<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Use_Function\">Use Function<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Read_from_an_Input\">Read from an Input<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Read_From_a_File\">Read From a File<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#map_Function\">map Function<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#pluck_Function\">pluck Function<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#More_Complex\">More Complex<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Operators\">Operators<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#RelationalEquality_Operators\">Relational\/Equality Operators<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Logical_Operators\">Logical Operators<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Prepend_or_Append_Operators_for_Arrays\">Prepend or Append Operators for Arrays<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Flow_Controls\">Flow Controls<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#do\">do<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#ifelse_ifelse\">if\/else if\/else<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<span id=\"Quick_Start\"><h2>Quick Start<\/h2><\/span>\n<span id=\"Prerequisites\"><h3>Prerequisites<\/h3><\/span>\n<p>* Studio 7<\/p>\n<span id=\"Setup_a_Project\"><h3>Setup a Project<\/h3><\/span>\n<p>* File &gt; New &gt; Mule Project<br \/>\n&#8211; Project Name: testscript<br \/>\n* Drag Transform Message to canvas<\/p>\n<span id=\"Concatenate_String\"><h3>Concatenate String<\/h3><\/span>\n<span id=\"\"><h3><\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\n---\n{\n\tmyString: (\"hello\" ++ \" world\")\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"myString\": \"hello world\"\n}\n<\/pre>\n<span id=\"JSON_to_XML\"><h3>JSON to XML<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/xml\n---\n{\n\t\"myString\": (\"hello\" ++ \" world\")\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\"><!--?xml version='1.0' encoding='windows-1252'?-->\n<mystring>hello world<\/mystring>\n<\/pre>\n<span id=\"Supported_Data_Types\"><h3>Supported Data Types<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\n---\n{\n  \/*\n   * A multi-line\n   * comment here.\n   *\/\n  myString: \"hello world\",\n  myNumber: 123,\n  myFloatingPointNumber: 123.456,\n  myVeryBigNumber: 12341234134123412341234123,\n  myDate: |2018-12-07|,\n  myTime: |11:55:56|,\n  myDateTime: |2018-10-01T23:57:59-03:00|,\n  myBoolean: true,\n  myArray: [ 1, 2, 3, 5, 8],\n  myMixedArray: [ 1, 2, \"blah\", { hello: \"there\" } ],\n  myObjectKeyValuePair: { innerKey: \"innerValue\" },\n  myObjectWithConditionalField: { a : { b : 1, ( c : 2 ) if true, (d : 4) if false } },\n  myNull: null,\n  myBinary: \"abcd1234123\" as Binary\n  \/\/A one-line comment here.\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"myString\": \"hello world\",\n  \"myNumber\": 123,\n  \"myFloatingPointNumber\": 123.456,\n  \"myVeryBigNumber\": 12341234134123412341234123,\n  \"myDate\": \"2018-12-07\",\n  \"myTime\": \"11:55:56\",\n  \"myDateTime\": \"2018-10-01T23:57:59-03:00\",\n  \"myBoolean\": true,\n  \"myArray\": [\n    1,\n    2,\n    3,\n    5,\n    8\n  ],\n  \"myMixedArray\": [\n    1,\n    2,\n    \"blah\",\n    {\n      \"hello\": \"there\"\n    }\n  ],\n  \"myObjectKeyValuePair\": {\n    \"innerKey\": \"innerValue\"\n  },\n  \"myObjectWithConditionalField\": {\n    \"a\": {\n      \"b\": 1,\n      \"c\": 2\n    }\n  },\n  \"myNull\": null,\n  \"myBinary\": \"abcd1234123\"\n}\n<\/pre>\n<span id=\"Define_and_Use_Var_as_Input\"><h3>Define and Use Var as Input<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\nvar myjson = {\"hello\": \"world\"}\noutput application\/json\n---\nmyjson\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"hello\": \"world\"\n}\n<\/pre>\n<span id=\"Use_Function\"><h3>Use Function<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\nvar myjson = {\n\t\"a\": avg([1,1000]),\n\t\"b\": avg([1,2,3])\n}\noutput application\/json\n---\nmyjson\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"a\": 500.5,\n  \"b\": 2.0\n}\n<\/pre>\n<span id=\"Read_from_an_Input\"><h3>Read from an Input<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\nvar myRead = read(\"<car><color>Read<\/color><\/car>\", \"application\/xml\")\noutput application\/json\n---\n{\n\tmySelection: myRead.car\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"mySelection\": {\n    \"color\": \"Read\"\n  }\n}\n<\/pre>\n<span id=\"Read_From_a_File\"><h3>Read From a File<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\n---\nreadUrl(\"classpath:\/\/myJason.json\", \"application\/json\")\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"hello\": \"world\"\n}\n<\/pre>\n<span id=\"map_Function\"><h3>map Function<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\n---\n{\n\t( \/\/Need this to evaluate\n\t\t[\"a\",\"b\",\"c\"] map ((value, index) -&gt; {\n\t\t\t(index): value\n\t\t})\n\t)\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"0\": \"a\",\n  \"1\": \"b\",\n  \"2\": \"c\"\n}\n<\/pre>\n<span id=\"pluck_Function\"><h3>pluck Function<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\n---\n{\n  \"0\": \"a\",\n  \"1\": \"b\",\n  \"2\": \"c\"\n} pluck ((value) -&gt; value)\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">[\n  \"a\",\n  \"b\",\n  \"c\"\n]\n<\/pre>\n<span id=\"More_Complex\"><h3>More Complex<\/h3><\/span>\n<p>* Script<\/p>\n<pre lang=\"json\">%dw 2.0\nvar myVar = [\n  { bookId: 101,\n    title: \"world history\",\n    price: \"19.99\"\n  },\n  {\n    bookId: 202,\n    title: 'the great outdoors',\n    price: \"15.99\"\n  }\n]\nvar myVar2 = [\n  {\n    bookId: 101,\n    author: \"john doe\"\n  },\n  {\n    bookId: 202,\n    author: \"jane doe\"\n  }\n]\noutput application\/json\n---\nmyVar map (item, index) -&gt; using (id = item.bookId) {\n\t\"id\" : id,\n\t\"topic\" : item.title,\n\t\"cost\" : item.price as Number,\n\t(myVar2 filter ($.*bookId contains id) map (item) -&gt; {\n\t\tauthor : item.author\n\t})\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">[\n  {\n    \"id\": 101,\n    \"topic\": \"world history\",\n    \"cost\": 19.99,\n    \"author\": \"john doe\"\n  },\n  {\n    \"id\": 202,\n    \"topic\": \"the great outdoors\",\n    \"cost\": 15.99,\n    \"author\": \"jane doe\"\n  }\n]\n<\/pre>\n<span id=\"Operators\"><h2>Operators<\/h2><\/span>\n<span id=\"RelationalEquality_Operators\"><h3>Relational\/Equality Operators<\/h3><\/span>\n<pre lang=\"bash\">==\n~=\n&lt;\n&gt;\n&lt;=\n&gt;=\n<\/pre>\n<span id=\"Logical_Operators\"><h3>Logical Operators<\/h3><\/span>\n<pre lang=\"bash\">not # (not true or true) is same as (not(true or true)) which evaluates to false\n!   # (!true or true) is same as (false or true) which evaluates to true\nand\nor\n<\/pre>\n<span id=\"Prepend_or_Append_Operators_for_Arrays\"><h3>Prepend or Append Operators for Arrays<\/h3><\/span>\n<pre lang=\"json\">&gt;&gt; # Prpends to left side of array so 1 &gt;&gt; [2] results in [1,2]\n&lt;&lt; # Appends to right side of array so 2 &lt;&lt; [1] results in [1,2]\n+  # [1] + 2 results in [1,2]. Array is always on the left-hand side of the operator\n<\/pre>\n<span id=\"Flow_Controls\"><h2>Flow Controls<\/h2><\/span>\n<span id=\"do\"><h3>do<\/h3><\/span>\n<p>* Creates a scope<\/p>\n<pre lang=\"json\">%dw 2.0\noutput application\/json\nfun myfun() = do {\n\tvar msg = \"Hello World!\"\n\t---\n\tmsg\n}\nfun sayhi(s: String) = do {\n\tvar msg = \"Hello \" ++ s ++ \"!\"\n\t---\n\tmsg\n}\n---\n{\n\tresult: myfun(),\n\tresult: sayhi(\"Jimmy\")\n}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"result\": \"Hello World!\",\n  \"result\": \"Hello Jimmy!\"\n}<\/pre>\n<span id=\"ifelse_ifelse\"><h3>if\/else if\/else<\/h3><\/span>\n<pre lang=\"bash\">%dw 2.0\noutput application\/json\n---\nif (true)\n\t{ result: \"passed\"}\nelse\n\t{ result: \"failed\"}\n<\/pre>\n<p>* Output<\/p>\n<pre lang=\"json\">{\n  \"result\": \"passed\"\n}\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"https:\/\/docs.mulesoft.com\/mule-runtime\/4.2\/intro-dataweave2\">Introduction to Mule 4: DataWeave 2.0<\/a><br \/>\n* <a href=\"https:\/\/docs.mulesoft.com\/mule-runtime\/4.2\/dataweave-quickstart\">DataWeave Quickstart<\/a><br \/>\n* <a href=\"https:\/\/docs.mulesoft.com\/mule-runtime\/4.2\/dataweave\">DataWeave Language<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Quick Start Prerequisites * Studio 7 Setup a Project * File &gt; New &gt; Mule Project &#8211; Project Name: testscript * Drag Transform Message to canvas Concatenate String * Script %dw 2.0 output application\/json &#8212; { myString: (&#8220;hello&#8221; ++ &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=12343\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[819],"tags":[816,815],"class_list":["post-12343","post","type-post","status-publish","format-standard","hentry","category-mule4","tag-dataweave2","tag-mule4"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-3d5","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12343"}],"version-history":[{"count":4,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12343\/revisions"}],"predecessor-version":[{"id":12359,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12343\/revisions\/12359"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}