Forum Discussion

viji28's avatar
viji28
New Contributor
4 years ago
Solved

Retrieve the key value by passing another key value in json

My Json looks like this πŸ™‚ { β€œmain” :[ { β€œCM”: β€œT”, β€œTM”: β€œβ€ }, { β€œCM”: β€œ100000”, β€œTM”: β€œ9999” }, { β€œCM”: β€œ200000”, β€œTM”: β€œβ€ } ] } This is stored in an test.expr file. ...
  • del's avatar
    4 years ago

    @viji28,

    If you have the liberty to restructure your test.expr expression library file, I would recommend moving to the match control operator syntax for reduced code and simpler reference. For the test.expr file, you could change it to:

    {
        "main": cm => match cm {
           "T" => "",
           "100000" => "9999",
           "200000" => "",
           _ => ""
        }
    }
    

    After, you can use a simpler reference:

    lib.test.main($CM)
    

    However, if you can’t make a major change to the structure for some reason, but can adapt it to an arrow function, you can incorporate bojanvelevski’s suggestion in the expression library and still use the simpler lib.test.main($CM) reference. The file might look something like this:

    { 
    	"main": (cm =>
    		[
    			{
    				"CM": "T",
    				"TM": ""
    			},
    			{
    				"CM": "100000",
    				"TM": "9999"
    			},
    			{
    				"CM": "200000",
    				"TM": ""
    			}
    		].filter(x=> x.CM==cm)[0].TM)
    }
    

    There are other restructuring options, as well.