0

I am trying to deserialize json to Map< string,Map< string,decimal>>.

json

{
"redrose": {
    "11-Jan-2019": "$1.85",
    "16-Jan-2019": "$1.85",
    "17-Jan-2019": "$1.85",
    "18-Jan-2019": "$1.85",
    "23-Jan-2019": "$1.85",
    "24-Jan-2019": "$1.85",
    "25-Jan-2019": "$1.85",
    "30-Jan-2019": "$1.85",
    "31-Jan-2019": "$1.85",
    "01-Feb-2019": "$1.85",
    "06-Feb-2019": "$1.85",
    "07-Feb-2019": "$1.85",
    "08-Feb-2019": "$1.85",
    "14-Jan-2019": "$2.25",
    "15-Jan-2019": "$1.85",
    "21-Jan-2019": "$2.25",
    "22-Jan-2019": "$1.85",
    "28-Jan-2019": "$2.25",
    "29-Jan-2019": "$1.85",
    "04-Feb-2019": "$2.25",
    "05-Feb-2019": "$1.85",
    "11-Feb-2019": "$2.25",
    "12-Feb-2019": "$2.25",
    "10-Jan-2019": "$1.85"
},
"whiterose": {
    "10-Jan-2019": "$0.61",
    "11-Jan-2019": "$0.61",
    "15-Jan-2019": "$0.61",
    "16-Jan-2019": "$0.61",
    "17-Jan-2019": "$0.61",
    "18-Jan-2019": "$0.61",
    "22-Jan-2019": "$0.61",
    "23-Jan-2019": "$0.61",
    "24-Jan-2019": "$0.61",
    "25-Jan-2019": "$0.61",
    "29-Jan-2019": "$0.61",
    "30-Jan-2019": "$0.61",
    "31-Jan-2019": "$0.61",
    "01-Feb-2019": "$0.61",
    "05-Feb-2019": "$0.61",
    "06-Feb-2019": "$0.61",
    "07-Feb-2019": "$0.61",
    "08-Feb-2019": "$0.61"
 }
}

I need to store this deserialize json in Map< string,Map< string,decimal>>

1

Why not replace All $ with '' and then parse it as Map<String, Map<String, Decimal>> result

String inputJSON = '{"redrose":{"11-Jan-2019":"$1.85"},"whiterose":{"10-Jan-2019":"$0.61"}}';

inputJSON = inputJSON.replaceAll('$','');

Map<String, Map<String, Decimal>> result = (Map<String, Map<String, Decimal>>)JSON.deserialize(inputJSON, Map<String, Map<String, Decimal>>.class);
2

You cannot do the deserialisation that you want in one step because the values you want to be decimals are actually strings like "$0.61". If they were raw numbers, it would be quite simple e.g.

String s = '{"redrose": {"11-Jan-2019": 1.85,"16-Jan-2019": 1.85} }';

Map<String, Map<String, Decimal>> result = (Map<String, Map<String, Decimal>>)JSON.deserialize(s, Map<String, Map<String, Decimal>>.class);

System.debug(result);

If efficiency isn't all that important, the easiest way would be to deserialize into a Map<String, Object>, then traverse that Map to create the one you want. If you have to deal with large amounts of data, you could use JSON.createParser(string) and convert the data at the same time as doing the parsing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.