top of page

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

Writer's picture: Alex MartinezAlex Martinez


 

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

Clue #2

Clue #3

Clue #4



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

Solution #2


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


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








946 views8 comments

8 comentários


sabrina collins
sabrina collins
09 de out. de 2024

When I sent my research proposal to Phdresearchproposal research proposal service, I had great hopes for their help, but their professionalism and expertise well above my expectations. Their proposal showed that they understood my company well, and they finished the job to my satisfaction. Their attention to detail had no bounds. Everything was so well-organized that even the most complex difficulties were easy to understand. Their ability to meet my very tight deadline has greatly reduced my worries. Phdresearchproposal is the place to go if you need trustworthy help from experts.

Curtir

Sudiptaa
Sudiptaa
13 de jun. de 2023

Used tree: %dw 2.0 output application/json import * from dw::util::Tree --- payload mapLeafValues ((value, path) -> if(path.selector contains 'thisname') value else upper(value) )

Curtir
Alex Martinez
Alex Martinez
13 de jun. de 2023
Respondendo a

Woohoo! Super simple. Love it! Thank you for sharing :D

Curtir

Kenneth Brennan
13 de jun. de 2023

I went with a recursive solution: %dw 2.0 output application/json fun capitalizeObject(item: Object) = do{ fun matchItem(item) = item match{ case item is Object -> item mapObject ((value, key, index) -> if(key as String == 'thisname') {(key): value} else {(key): matchItem(value)}) case item is Array -> item map ((item, index) -> matchItem(item)) case item is String -> upper(item) } --- matchItem(item) } --- capitalizeObject(payload)

Curtir
Kenneth Brennan
13 de jun. de 2023
Respondendo a

That pesky stack overflow, I'm not sure how to rewrite this in the same format using tail recursion and match, hard to conceptualize. Might give it another go when I get some free time. Thanks for posting these challenges, I enjoyed this one!

Curtir

Shyam Raj Prasad
Shyam Raj Prasad
13 de jun. de 2023

My solution is similar to yours with extra type check of string.


%dw 2.0 output application/json import * from dw::util::Tree --- payload mapLeafValues ((value, path) -> if(!(path[-1].selector == "thisname") and value is String) upper(value) else value )

Curtir
Alex Martinez
Alex Martinez
13 de jun. de 2023
Respondendo a

always the first to comment. nice!!

Curtir
bottom of page