1

I'm trying to selectively update some values in a nested hierarchical data structure.

I have nested input data such as

 var flights = 
{
  "flights": {
    "flight": {
      "airline-name": "american",
      "flight-code": "AA103",
      "plane-type": null,
      "longitude": "33.94250107",
      "price": "+750.00",
      "destination": {
        "airport-name": "los angeles international airport",
        "city": "los angeles",
        "altitude": "125",
        "longitude":"33.94250107"
    }
  },
    
  "flight": {
      "available-seats": "+18.00",
      "longitude": "33.94250107",
      "airline-name": "delta",
      "plane-type": "boeing 747",
      "destination": {
        "airport-name": "los angeles international airport",
        "longitude": "33.94250107"
      }
    }
 
 }
}

I want to mask all the longitudes at all levels with one case statement.

flights dw::util::Values::update {
  case dest at .flights.*flight.*destination.longitude  -> "******"
  case ptype at .flights.*flight.*"plane-type" if((ptype ~= null)) -> "*******"
}

only matches inside the destination value.

{
  "flights": {
    "flight": {
      "airline-name": "american",
      "flight-code": "AA103",
      "plane-type": "*******",
      "longitude": "33.94250107",
      "price": "+750.00",
      "destination": {
        "airport-name": "los angeles international airport",
        "city": "los angeles",
        "altitude": "125",
        "longitude": "******"
      }
    },
    "flight": {
      "available-seats": "+18.00",
      "longitude": "33.94250107",
      "airline-name": "delta",
      "plane-type": "boeing 747",
      "destination": {
        "airport-name": "los angeles international airport",
        "longitude": "******"
      }
    }
  }
}

Also, is there a way to do something like flights..*destination instead of hardcoding the path?

1
  • I think you would have to do .*longitude instead of .longitude. However, easier would be to just use the mask function. Commented Oct 27, 2020 at 23:18

1 Answer 1

3

You can try using the mask function:

%dw 2.0
output application/json
import * from dw::util::Values
var flights = 
{
  "flights": {
    "flight": {
      "airline-name": "american",
      "flight-code": "AA103",
      "plane-type": null,
      "longitude": "33.94250107",
      "price": "+750.00",
      "destination": {
        "airport-name": "los angeles international airport",
        "city": "los angeles",
        "altitude": "125",
        "longitude":"33.94250107"
    }
  },
    
  "flight": {
      "available-seats": "+18.00",
      "longitude": "33.94250107",
      "airline-name": "delta",
      "plane-type": "boeing 747",
      "destination": {
        "airport-name": "los angeles international airport",
        "longitude": "33.94250107"
      }
    }
 
 }
}
---
(flights mask "longitude" with "****") mask "plane-type" with "****"

Output:

{
  "flights": {
    "flight": {
      "airline-name": "american",
      "flight-code": "AA103",
      "plane-type": "****",
      "longitude": "****",
      "price": "+750.00",
      "destination": {
        "airport-name": "los angeles international airport",
        "city": "los angeles",
        "altitude": "125",
        "longitude": "****"
      }
    },
    "flight": {
      "available-seats": "+18.00",
      "longitude": "****",
      "airline-name": "delta",
      "plane-type": "****",
      "destination": {
        "airport-name": "los angeles international airport",
        "longitude": "****"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.