top of page

DataWeave programming challenge #7: Modify certain values from a JSON structure

Updated: Aug 31, 2023



Other posts from this series:

In this post:



Try to solve this challenge on your own to maximize learning. We recommend you refer to the DataWeave documentation only. Try to avoid using Google or asking others so you can learn on your own and become a DataWeave expert!


💡 Tip: Instead of trying to write the whole code yourself, browse in the documentation for any function that would do the work for you 👀



Input


Consider the following JSON input payload:


{
    "name": "a",
    "object": {
      "name": "b",
      "l": [
        {
          "other": "c",
          "list": [
            
          ]
        },
        {
          "thisname": "def",
          "list": [
            {
              "this": "500e",
              "l": [
                {
                  "finalname": "f"
                },
                {
                  "finalname": "ghijk"
                }
              ]
            }
          ]
        }
      ]
    },
    "array": [
      {
        "thisname": "h"
      },
      {
        "xyz": "abc123"
      }
    ]
  }


Explanation of the problem


Create a DataWeave script that will update all the values to uppercase, except the ones in which the field equals thisname.


For example, the first field name with the value "a" has to be transformed to "A". However, the field thisname with the value of "def" should stay the same.



Expected output


In this case, the expected output would be:


{
  "name": "A",
  "object": {
    "name": "B",
    "l": [
      {
        "other": "C",
        "list": [
          
        ]
      },
      {
        "thisname": "def",
        "list": [
          {
            "this": "500E",
            "l": [
              {
                "finalname": "F"
              },
              {
                "finalname": "GHIJK"
              }
            ]
          }
        ]
      }
    ]
  },
  "array": [
    {
      "thisname": "h"
    },
    {
      "xyz": "ABC123"
    }
  ]
}




Clues


If you're stuck with your solution, feel free to check out some of these clues to give you ideas on how to solve it!


Clue #1

Check out the Tree module to see if any of those functions is useful for this case.

Clue #2

Check out the mapLeafValues function.

Clue #3

Use the upper function to transform the strings to uppercase.

Clue #4

Use path[-1].selector to retrieve the name of each value's field and compare it with a given string value.



Answer


If you haven't solved this challenge yet, we encourage you to keep trying! It's ok if it's taking longer than you thought. We all have to start somewhere ✨ Check out the clues and read the docs before giving up. You got this!! 💙


There are many ways to solve this challenge, but you can find here some solutions we are providing so you can compare your result with us.


Solution #1

%dw 2.0

import mapLeafValues from dw::util::Tree

output application/json

---

payload mapLeafValues ((value, path) ->

if (path[-1].selector == "thisname") value

else upper(value)

)

Solution #2

%dw 2.0

import mapLeafValues from dw::util::Tree

output application/json

---

payload mapLeafValues

if ($$[-1].selector == "thisname") $

else upper($)


Feel free to comment your code below for others to see! 😄


Subscribe to receive notifications as soon as new content is published ✨








20 Comments


budi kage
budi kage
Oct 27

Link Slot Gacor The best online game platform

Like

budi kage
budi kage
Oct 27

Bejototo

Bejototo

Situs Toto

Situs Toto adalah salah satu bandar game terbaik dengan hadiah yang mencapai ratusan juta rupiah

Like

budi kage
budi kage
Oct 27

Toto

Situs Idrtoto

Toto Togel adalah salah satu link situs togel terbesar dan terpercaya no 1 di Indonesia

Like

budi kage
budi kage
Oct 27

Akuntoto Login

Akun Toto

Akuntoto Link

Akuntoto Login adalah salah satu bandar togel online dengan hadiah jackpot terbesar serta pasaran yang lengkap

Like

budi kage
budi kage
Oct 27

Amavi5d Daftar

Amavi 5D

Situs Amavi5d

Toto adalah situs togel keluaran angka & prediksi jitu terbaik di indonesia

Like

Join our mailing list

Thanks for subscribing!

  • Youtube
  • GitHub
  • LinkedIn
bottom of page