Parse the JSON response of external web api.

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.
Best Regards
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.

please like / agree / verify my answer, if it was helpful for you. thanks.

Comments

  • ftorneroftornero Member Posts: 522
    Well you never change the var CurrentObject since the first time, so you only process the "webOrderHeader".

    I've made some changes in your code:

    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);

    FormatChar := '"{}';
    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

    qPos := STRPOS(ValuePair, '[');
    IF qPos > 0 THEN
    ValuePair := COPYSTR(ValuePair, qPos+1);

    qPos := STRPOS(ValuePair, 'webOrderHeader');
    IF qPos > 0 THEN BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16);
    CurrentObject := 'webOrderHeader';
    END;

    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;
    'WebOrderLine': // End of Header
    CurrentObject := 'webOrderLine';
    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;



    Regards
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    edited 2017-01-20
    Thanks @ftornero for tweaking my code. It works perfectly fine :)
    Can you please tell me what this line is doing?

    IF qPos > 0 THEN
    BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16); //this one?
    CurrentObject := 'webOrderHeader';
    END;
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    @ftornero can you please help me in tweaking the code for this json response?

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


    Here is my code:


    LOCAL TestParseJsonResponse(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);
    FormatChar := '"{}';
    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
    qPos := STRPOS(ValuePair, '[');
    IF qPos > 0 THEN
    ValuePair := COPYSTR(ValuePair, qPos+1);

    qPos := STRPOS(ValuePair, 'webOrderHeader');

    IF qPos > 0 THEN
    BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16);
    CurrentObject := 'webOrderHeader';
    END;

    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':
    WebOrderHeader."Order Date" := TODAY;
    'TotalPrice':
    BEGIN
    EVALUATE(WebOrderHeader."Total Price",CurrentValue);
    WebOrderHeader.MODIFY;
    CurrentObject := 'webOrderLine';
    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;
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ftorneroftornero Member Posts: 522
    zohaibu95 wrote: »
    Thanks @ftornero for tweaking my code. It works perfectly fine :)
    Can you please tell me what this line is doing?

    IF qPos > 0 THEN
    BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16); //this one?
    CurrentObject := 'webOrderHeader';
    END;

    It's hardcoded, but it's to skip webOrderHeader plus the ", total 16 chars

    qPos := STRPOS(ValuePair, 'webOrderHeader');

    Regards
  • ftorneroftornero Member Posts: 522
    zohaibu95 wrote: »
    @ftornero can you please help me in tweaking the code for this json response?

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


    Here is my code:


    LOCAL TestParseJsonResponse(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);
    FormatChar := '"{}';
    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
    qPos := STRPOS(ValuePair, '[');
    IF qPos > 0 THEN
    ValuePair := COPYSTR(ValuePair, qPos+1);

    qPos := STRPOS(ValuePair, 'webOrderHeader');

    IF qPos > 0 THEN
    BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16);
    CurrentObject := 'webOrderHeader';
    END;

    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':
    WebOrderHeader."Order Date" := TODAY;
    'TotalPrice':
    BEGIN
    EVALUATE(WebOrderHeader."Total Price",CurrentValue);
    WebOrderHeader.MODIFY;
    CurrentObject := 'webOrderLine';
    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;

    You can do this changes:

    >>FormatChar := '"{}';

    <<FormatChar := '"{}]';

    >> qPos := STRPOS(ValuePair, '[');
    >> IF qPos > 0 THEN
    >> ValuePair := COPYSTR(ValuePair, qPos+1);

    << qPos := STRPOS(ValuePair, '[');
    << IF qPos > 0 THEN BEGIN
    << ValuePair := COPYSTR(ValuePair, qPos+1);
    << CurrentObject := 'webOrderLine';
    << END;

    Regards.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    @ftornero it is not working as expected. Its just created one line in header and no lines in Lines table. :(
    Can you please tell me the overall algorithm of this logic?
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ftorneroftornero Member Posts: 522
    It works for me,

    This is all the code:

    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);

    FormatChar := '"{}]';
    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

    qPos := STRPOS(ValuePair, '[');
    IF qPos > 0 THEN BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+1);
    CurrentObject := 'webOrderLine';
    END;


    qPos := STRPOS(ValuePair, 'webOrderHeader');
    IF qPos > 0 THEN BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16);
    CurrentObject := 'webOrderHeader';
    END;

    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;
    'WebOrderLine': // End of Header
    BEGIN
    CurrentObject := 'webOrderLine';
    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;


  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    @ftornero yeah its working great. I was missing one check. Thanks alot :).
    Last thing, can you please share the algorithm which you have followed for this? :)
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    @ftornero i have few questions regarding this code

    how is FormatChar variable is working?

    Also what is hte purpose of qPos variable??
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ftorneroftornero Member Posts: 522
    I only have changed a little your algortithm.


    // Here I tested if the ValuePair have a [, meaning is the start of a webOrderLine
    // So I skip this part and chabge the CurrentObject.
    qPos := STRPOS(ValuePair, '[');
    IF qPos > 0 THEN BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+1);
    CurrentObject := 'webOrderLine';
    END;


    //Here I tested if ValuePair have webOrderHeader, so is the start of a
    // webOrderHeader and I skip this part too.
    qPos := STRPOS(ValuePair, 'webOrderHeader');
    IF qPos > 0 THEN BEGIN
    ValuePair := COPYSTR(ValuePair, qPos+16);
    CurrentObject := 'webOrderHeader';
    END;

    The rest it's yours.

    Regards
  • ftorneroftornero Member Posts: 522
    zohaibu95 wrote: »
    @ftornero i have few questions regarding this code

    how is FormatChar variable is working?

    Also what is hte purpose of qPos variable??

    I'm using FormatChar because is in your original code and I understand must be a text constant but how I don't know its valueI created a text variable and asigned the value of '{}' and later I changed to '{}]', to avoid a problem with the CurrentValue.

    And the purpose of qPos it's only to check if the value tested with STRPOS exists and in this case it's used to skip that value.

    Regards
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Thanks alot @ftornero :) If i need help i will bother you again. :)
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ftorneroftornero Member Posts: 522
    No problem
  • KishormKishorm Member Posts: 921
    Have you considered converting the Json format to XML and then processing that instead?

    If you'd like to do that then have a look at Codeunit 1273 "Get Json Structure" (in NAV 2016) - it has a function called JsonToXML()
  • RockWithNAVRockWithNAV Member Posts: 1,139
    Codeunit is 1237 not 1273.
  • HannesHolstHannesHolst Member Posts: 119
    Hey there,

    check this resource here:
    http://www.dynamics.is/?p=2303

    including code-example for download.

    Cheers,
    Hannes
  • EvREvR Member Posts: 178
    Oh wow... what a bunch of unnecessary complexity and manual parsing!
    Just create a .NET class of the object you want to deserialize, and use Json.NET to deserialize the repsonse. 4 lines of NAV code, max! ;)
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Kishorm wrote: »
    Have you considered converting the Json format to XML and then processing that instead?

    If you'd like to do that then have a look at Codeunit 1273 "Get Json Structure" (in NAV 2016) - it has a function called JsonToXML()

    JSON format is more common and more efficient now a days. Converting it into xml will cause an overhead.

    I have create a manual parser for json. btw a good way is to create custom DLL and parse json there and return the extracted data back to NAV.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    EvR wrote: »
    Oh wow... what a bunch of unnecessary complexity and manual parsing!
    Just create a .NET class of the object you want to deserialize, and use Json.NET to deserialize the repsonse. 4 lines of NAV code, max! ;)

    @EvR i know about that ;) i have created one for my old client but i am avoiding .NET as last time when i was deploying the solution i have faced a lot of challenges specially when i have to reschedule the job using JOB Queues. That is why i was trying to parse the JSON in C/AL.

    Btw i have solved the issue :D thanks to @ftornero . :)
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    JSON is much common now a days specially for RESTful API's. Microsoft should provide default codeunit for json parsing in c/al as well. What do you think guys?
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Hey there,

    check this resource here:
    http://www.dynamics.is/?p=2303

    including code-example for download.

    Cheers,
    Hannes

    I have seen this code and try to use it but it is using the "Data Exch. Field" table. and when i try to parse my json response it is giving some error. So after trying to understand that code i thought creating custom parse is the good way to go.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
Sign In or Register to comment.