Quantcast
Channel: NAV Three Tier — mibuso.com
Viewing all articles
Browse latest Browse all 10032

Parse the JSON response of external web api.

$
0
0
Hi guys,
I have created an external web api which returns the data in JSON format. Here is the sample response:

[{"webOrderHeader":{"OrderNo":1,"OrderDate":"2017-01-18T00:00:00","TotalPrice":100.0,"WebOrderLine":null},"webOrderLine":[{"No":1,"OrderNo":1,"ItemNo":"ABC1","Quantity":1,"UnitPrice":50.0,"WebOrderHeader":null},{"No":2,"OrderNo":1,"ItemNo":"ABC1","Quantity":1,"UnitPrice":50.0,"WebOrderHeader":null}]},{"webOrderHeader":{"OrderNo":2,"OrderDate":"2017-01-19T00:00:00","TotalPrice":100.0,"WebOrderLine":null},"webOrderLine":[{"No":3,"OrderNo":2,"ItemNo":"ABC1","Quantity":1,"UnitPrice":100.0,"WebOrderHeader":null}]},{"webOrderHeader":{"OrderNo":3,"OrderDate":"2017-01-18T00:00:00","TotalPrice":100.0,"WebOrderLine":null},"webOrderLine":[{"No":4,"OrderNo":3,"ItemNo":"XYZ1","Quantity":1,"UnitPrice":100.0,"WebOrderHeader":null}]},{"webOrderHeader":{"OrderNo":4,"OrderDate":"2017-01-20T00:00:00","TotalPrice":100.0,"WebOrderLine":null},"webOrderLine":null},{"webOrderHeader":{"OrderNo":5,"OrderDate":"2017-01-21T00:00:00","TotalPrice":100.0,"WebOrderLine":null},"webOrderLine":null}]

You can use this utility http://jsonviewer.stack.hu/ to see the json response in a user friendly way.

Now the question is, I want to parse this JSON response and add the data in my NAV tables. So for this, i have write a custom code which basically parses the above response. The code are as under:

LOCAL ParseJsonResponse(Value : Text)

p := 0;
x := 1;

IF STRPOS(Value,'[]') > 0 THEN EXIT;

CurrentObject := COPYSTR(Value,STRPOS(Value,'{')+1,STRPOS(Value,':'));
Value := COPYSTR(Value,STRLEN(CurrentObject)+1);

CurrentObject := DELCHR(CurrentObject,'=',FormatChar);
CurrentObject := DELCHR(CurrentObject,'=',':');
Value := DELCHR(Value,'=','{}');
l := STRLEN(Value);

WHILE p < l DO BEGIN

ValuePair := SELECTSTR(x,Value); // get comma separated pairs of values and element names

p := STRPOS(Value,ValuePair)+STRLEN(ValuePair); // move pointer to the end of the current pair in Value

ValuePair := DELCHR(ValuePair,'=',FormatChar);

CurrentElement := COPYSTR(ValuePair,1,STRPOS(ValuePair,':'));
CurrentElement := DELCHR(CurrentElement,'=',':');

CurrentValue := COPYSTR(ValuePair,STRPOS(ValuePair,':'));
CurrentValue := DELCHR(CurrentValue,'=',':');

CASE CurrentObject OF
'webOrderHeader':
CASE CurrentElement OF
'OrderNo':
// must be a new record or lookup the existing record
BEGIN
WebOrderHeader.SETFILTER("Order No.",CurrentValue);
EVALUATE(RecId,CurrentValue);
IF NOT WebOrderHeader.FINDFIRST THEN BEGIN
WebOrderHeader.INIT;
WebOrderHeader."Order No." := FORMAT(RecId);
WebOrderHeader.INSERT;
END;
END;
'OrderDate':
//EVALUATE(WebOrderHeader."Order Date",CurrentValue);
WebOrderHeader."Order Date" := TODAY;
'TotalPrice':
BEGIN
EVALUATE(WebOrderHeader."Total Price",CurrentValue);
WebOrderHeader.MODIFY;
END;

END;

'webOrderLine':
CASE CurrentElement OF
'No':
// must be a new record or lookup the existing record
BEGIN
EVALUATE(RecId,CurrentValue);
WebOrderLine.SETRANGE("Line No.",RecId);
IF NOT WebOrderLine.FINDFIRST THEN BEGIN
WebOrderLine.INIT;
WebOrderLine."Line No." := RecId;
WebOrderLine.INSERT;
END;
END;
'OrderNo':
WebOrderLine."Order No." := CurrentValue;
'ItemNo':
WebOrderLine."Item No." := CurrentValue;
'Quantity':
EVALUATE(WebOrderLine.Quantity,CurrentValue);
'UnitPrice':
BEGIN
EVALUATE(WebOrderLine."Unit Price",CurrentValue);
WebOrderLine.MODIFY;

END;
END;
END;
x := x+1; // next pair
END;


The issue which I m facing is the above code is perfectly parsing the webOrderHeader information but it is unable to parse the webOrderLine.

Please help me and if possible tweak the code.

Looking forward for the positive response.

Viewing all articles
Browse latest Browse all 10032

Trending Articles