Back to Examples

Match statement with maps

Match statement can be used to match maps. Patterns on the left hand side in a match statement can have variable parts that can be captured. Useful for working directly with json. Match semantics are open (may have fields other than those specified in the pattern).

import ballerina/io;
 
function execute(json j) {
   	match j {
   	    // A `match` statement can be used to match maps.
   	    // Patterns on the left hand side in a match statement can have variable
   	    // parts that can be captured.
   	    {command: "print", amount: var x} => {
   	        if x is int {
   	            io:println("Int: ", x);
   	        }
   	    }
	
   	    _ => {
   	        io:println("invalid command");
   	    }
   	}
}
 
public function main() {
   	execute({command: "print", amount: 100, status: "pending"});
   	execute({command: "print", amount: 10});
   	execute({command: "subtract", amount: 100});
}
$ bal run match_statement_with_maps.balInt: 100Int: 10invalid command

Related links

PreviousAccess optional JSON elements
NextConvert from user-defined type to JSON