Thursday, January 30, 2025

Parse JSON response using X++ to vairables

str output =//JSON String;
output = strRem(output, "[");
output = strRem(output, "]");

Map             jsonData;
jsonData = RetailCommonWebAPI::getMapFromJsonString(output);

container       Error,Log,LinkIds,currentReponse;
ListEnumerator  listEnumerator,listenum;
MapEnumerator   mapEnumerator;
mapEnumerator = jsonData.getEnumerator();
int                     AccNum;
int                     Name;
int                     Phone;
int                     address;
str                     errorTxt;
str                     logTxt;
str                     hasErrorStr;
while (mapEnumerator.moveNext())
{
    switch (mapEnumerator.currentKey())
    {
        case "Error":
            hasErrorStr = mapEnumerator.currentValue();
            break;
        case "Log":
            logTxt =  mapEnumerator.currentValue();
            break;
        case "CustomerDetails"://json array 
            currentReponse = mapEnumerator.currentValue();
            if(currentReponse)
            {
                for (int i=1; i <= conlen(currentReponse); i++)
                {
                    switch(conpeek(currentReponse, i))
                    {
                        case "AccNum":
                            AccNum = conPeek(currentReponse,i+1);
                            break;
                        case "Name":
                            Name = conPeek(currentReponse,i+1);
                            break;
                        case "Phone":
                            Phone = conPeek(currentReponse,i+1);
                            break;
                        case "address":
                            address = conPeek(currentReponse,i+1);
                            break;
                    }
                }
            }
            break;
    }
}

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8