Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Convert a json object to an Array list Using Rest

Posted on by 65

I'm using a post method to post Inventory data to an Https site. I was able to create a json string that looks like this example:

{"CustomerKey": "112221124545412","partCode": "Items-1","Description":"Chips"}

however I need the string look like the following:

{

  "parts": "[\r\n      {\r\n         \"CustomerKey\": \"e3704b8b-a09a-4143-8bb5-81bce8d5af43\",\r\n         \"PartCode\": \"Part 2\",\r\n         \"Description\": \"Part Description\",\r\n         \"AlternateDescription\": \"\",\r\n         \"UnitOfMeasure\": \"Each\",\r\n         \"EffectiveDate\": null,\r\n         \"ExpiryDate\": null,\r\n         \"ThresholdQuantity\": 10,\r\n         \"Location\": \"4\",\r\n         \"Certified\": false,\r\n         \"Taxable\": false,\r\n         \"UnitPrice\": 0,\r\n         \"Quantity\": 0,\r\n         \"OrderedbutNotReceived\": 0\r\n      },\r\n      {\r\n         \"CustomerKey\": \"e3704b8b-a09a-4143-8bb5-81bce8d5af43\",\r\n         \"PartCode\": \"Part 3\",\r\n         \"Description\": \"Part Description\",\r\n         \"AlternateDescription\": \"\",\r\n         \"UnitOfMeasure\": \"Each\",\r\n         \"EffectiveDate\": null,\r\n         \"ExpiryDate\": null,\r\n         \"ThresholdQuantity\": 10,\r\n         \"Location\": \"4\",\r\n         \"Certified\": false,\r\n         \"Taxable\": false,\r\n         \"UnitPrice\": 0,\r\n         \"Quantity\": 0,\r\n         \"OrderedbutNotReceived\": 0\r\n      }\r\n   ]"

}

 

I'm guessing I need to convert the Json object to an Array list!! is that true and how would I do that?

Thanks for the help 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Convert a json object to an Array list Using Rest

    Hello evbitas,

    Hope you have achieved your format.

    This is the format which I am trying to achieve in NAV 2016.  Can you please guide to achieve this and how did you achieve?

    {
    "bill": {
    "DocType": "INV",
    "DocNo": "104003",
    "DocDate": "08/02/2019",
    "ItemList": [
    {
    "ProductName": "TS1070I",
    "ProductDesc": "Manuafor eakers",
    "HsnCode": "85044090",
    "Quantity": "4",
    },
    {
    "ProductName": "TS1080I",
    "ProductDesc": "Manuafor eakers",
    "HsnCode": "85044090",
    "Quantity": "4",
    }
    ]
    }
    }

    Thanks,

  • Dietmar Wehinger Profile Picture
    Dietmar Wehinger 65 on at
    RE: Convert a json object to an Array list Using Rest

    Thanks Andrey for your help. Your answers helped me a lot.

    Thanks again..

  • Verified answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Convert a json object to an Array list Using Rest

    All objects are "Newtonsoft.Json.Linq.JObject"

    Array is Newtonsoft.Json.Linq.JArray

    By the way - I recommend you to take Codeunit 5459 JSON Management from NAV2018 and import it to your NAV2015 - it contains all the needed functions.

  • Ebitas Profile Picture
    Ebitas 70 on at
    RE: Convert a json object to an Array list Using Rest

    Thanks Andrey for the Info you provided. Yes I'm using Newtonsoft JSON dll.

    I'll try your solution and let you know.. Do you know what DotNet objects you used for JsonLineObject, JsonMainObject. , JsonArray ?

    Thanks again

  • Suggested answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Convert a json object to an Array list Using Rest

    Hi!

    I don't know, how you create JSON in NAV 2015 (perhaps it's Newtonsoft JSON dll), but in business central process is next:

    1. You create Array element (as Json object) :

    JsonLineObject.Add('CustomerKey', '112221124545412');

    .......

    JsonLineObject.Add('OrderedbutNotReceived', '123124');

     

    2. You add element to Array :

    JsonArray.Add(JsonLineObject);

    (probably you will do step 1 and 2 in REPEAT cycle)

    3. You create Main JsonObject with your array:

    JsonMainObject.Add('parts', JsonArray.AsToken.AsArray());

     

    4. You send JsonMainObject to API.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans