How to capture values in a JSON String.

shanabeywicremashanabeywicrema Posts: 39Member
edited 2019-02-18 in NAV Three Tier
Hello there,

Is there any way that I can copy JSON value and insert into dynamics nav table,
Here is the sample JSON response. I am doing this in Dynamics NAV 2013. Initially I copied values using SELECTSTR but is there any proper way to do this.
{
"result": {
"addresses": [
{
"city": "8888",
"address1": "*******",
"postalCode": "***",
"type": "BT",
"companyName1": "Z*****.",
"recordId": "BT",
"state": "NV",
"locationNumber": "**T",
"dunsNumber": "*T"
},
{
"country": "US",
"address2": "********",
"city": "New York",
"address1": "***************,
"postalCode": "**",
"type": "RE",
"companyName1": "***",
"recordId": "RE",
"state": "NY",
"locationNumber": "6*0",
"dunsNumber": "6*"
},

],


Thanks

Best Answers

Answers

  • shanabeywicremashanabeywicrema Posts: 39Member
    Hello ftornero ,
    Thank you so much for you effort. It is really worth. After all I got following error. Do you know where I missed. ?

    Microsoft Dynamics NAV

    This message is for C/AL programmers: Cannot create an instance of the following .NET Framework object: assembly Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, type Newtonsoft.Json.JsonConvert.
    OK
  • shanabeywicremashanabeywicrema Posts: 39Member
    I just copied into program 86. Not it is working, Thank you so much my problem has solved.
  • shanabeywicremashanabeywicrema Posts: 39Member
    Hi, Is there any way that we can pass text or bigtext instead of file ?
  • ftorneroftornero Posts: 187Member
    Yes @shanabeywicrema you need to modify the codeunit 1237 to deal with Text, something like this:

    921o4poo59kh.png

    You should duplicate the previous functions, change the names and parameters and then call the function GenerateStructureFromText with the Text var where is the JSON data.

    Regards.
  • krikikriki Posts: 8,660Member, Moderator
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
    NAV TechDays 2019: 21 & 22 November 2019, Antwerp (Belgium)
  • shanabeywicremashanabeywicrema Posts: 39Member
    Hello @ftornero
    I have done as your code. But When I pass the request, I am getting this error. Could you please guide me where I wrong .

    Microsoft Dynamics NAV

    A call to Newtonsoft.Json.JsonConvert.DeserializeXmlNode failed with this message: JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName. Path 'message', line 1, position 15538.
    OK
  • ftorneroftornero Posts: 187Member
    Hello @shanabeywicrema,

    Looks like the problem is with the Json text.

    Could you post how are you filling the text variable with the json data and the real json data ?

    Regards.
  • shanabeywicremashanabeywicrema Posts: 39Member
    Hello @ftornero ,
    I just call the rest web-service and call the relevant function,then I will get the response, Same response I will pass into GenerateStructureFromText

    Request := RequestClass.XMLHTTPRequestClass;
    Request.open('GET',MySetup."APIEndpoint"+'documents/order/'+ID,FALSE,'','');
    Request.setRequestHeader('Content-Type',ContentType);
    Request.setRequestHeader('sessionId',UserSessionID);
    Request.send('');
    IF Request.status = 200 THEN BEGIN
    GenerateStructureFromText(Request.responseText,TempXMLBuf,True);
    END;

    I use message to check the response text. I am getting the data.
  • ftorneroftornero Posts: 187Member
    Sorry again @shanabeywicrema,

    The right call is this one:
    GenerateStructureFromText('{"data":' + Request.responseText +'}',TempXMLBuf,True);
    

    You need to remove the second curly brace.

    Regards.
Sign In or Register to comment.