SBX - Search With Button

SBX - Forum Post Title

Json array

Microsoft Dynamics AX Forum

maram fraij asked a question on 17 May 2018 8:56 PM
My Badges

Question Status

Suggested Answer

I am working with ax data entity using Json.

I try to push a list of records to AX data base using Json array shown below  but the below error is faced 

 Json array:

[{"Key1":"Value1","Key2":Value } ,
{"Key1":"Value1","Key2":Value }]

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray

the question is how to pass a Json array 

Reply
Martin Dráb responded on 18 May 2018 1:40 AM
Suggested Answer

Please make sure you give us enough information; now I'll have to make quite a few assumptions.

I assume you're talking about a custom service which accepts a single argument of type array. And I assume the JSON string above is what you're trying to send to the service.

If so, your message isn't correct. Instead of using an array directly, you should provide an object with one property (with the same name as the name of the method argument). The value of the property will be the array.

Reply
maram fraij responded on 12 Jun 2018 9:04 AM
My Badges

Below  the code I use , the string json include the format of the array above

string json ; //formatted string

var data = Encoding.UTF8.GetBytes(json);

request.ContentLength = json.Length;

request.ContentType = "application/json";

var newStream = request.GetRequestStream();

newStream.Write(data, 0, data.Length);

newStream.Close();

var result = request.GetResponse();

Reply
Martin Dráb responded on 12 Jun 2018 9:12 AM

Did you at least try my solution?

Reply
maram fraij responded on 12 Jun 2018 9:16 AM
My Badges

I do not understand (you should provide an object with one property (with the same name as the name of the method argument)

My question is where I have to provide the object  and also what do you mean by (with the same name as the name of the method argument)

Reply
Martin Dráb responded on 12 Jun 2018 9:46 AM

Today you added a comment on my blog, but it seems you didn't read previous discussion. My explanation is there just above your comment. Let me copy it here for you:

I think you’re forgetting to identify the parameter.
If your parameter is int x, JSON will be { x: 2 }. If your parameter is List x, JSON will be { x: [ … ] }.
Therefore your actual JSON will be something like { startArray: [{ a : 2, b : 5 },{ a : 1, b : 3 },{ a : 9, b : 4 }]}.

Reply
Hatem.Mahdy responded on 13 Jun 2018 4:52 AM

We tried your suggested solution, but this error appears:

"The property 'startArray' does not exist on type 'Microsoft.Dynamics.DataEntities.PayTimeRegister'. Make sure to only use property names that are defined by the type."

And, as I know that StartArray refers to the JsonToken "[" , and EndArray refers to "]"

Note that your code is working prefect when we are sending a single item . but we need to send array of items at once.

Reply
maram fraij responded on 13 Jun 2018 5:42 AM
My Badges

Hi Martin

From AX point of view, each parameter is a field in AX data entity , is there any property on the data entity itself or data entity field  to make it accept posting a list of records

Reply
Martin Dráb responded on 13 Jun 2018 10:32 AM
Suggested Answer

Sorry, I thought you're talking about a custom service when you commented on my blog about custom services; it seems you actually mean an OData service.

I don't bother writing JSON string manually so I don't know the answer on the top of my head, but let me teach you how you can get it. Download the sample OData application from GitHub. Using the classes to do what you want, execute and observe the JSON sent to AX. And you're done.

Reply
Ada Ding responded on 13 Jun 2018 5:09 PM
My Badges

You may consider to Serialize the Json string and then use it to have a try. And as Martin mentioned, you can look at the Json string sending to Ax server using the demo. You can make use of develop tools in browser or Fiddler.

Reply
maram fraij responded on 21 Jun 2018 9:22 AM
My Badges

Hi Martin,

WebRequest  is used  and we try the idea used in the link below method (JsonServiceGroup1EchoContractListTest) , but unfortunately it does not work  

The  serialized json string is as the following:

{"input":[{   XXXXXX  }]},, XXXXXX   indicates our data entity properties .

The system does not accept "input" string at the beginning of the array.

github.com/.../ServiceGroup1Tests.cs

Reply
Martin Dráb responded on 21 Jun 2018 9:44 AM

I recommend you follow my last advice. It will clearly show you the right syntax, so you don't have to keep trying to guess it.

Sorry, I don't have currently time to do it for you, so you'll better off doing it by yourself.

Note that I have no idea what you mean by "the idea used in the link below method (JsonServiceGroup1EchoContractListTest)".

By the way, don't pay attention just to the input string, but also whether you're using the right HTTP operation etc.

Reply
Martin Dráb responded on 18 May 2018 1:40 AM
Suggested Answer

Please make sure you give us enough information; now I'll have to make quite a few assumptions.

I assume you're talking about a custom service which accepts a single argument of type array. And I assume the JSON string above is what you're trying to send to the service.

If so, your message isn't correct. Instead of using an array directly, you should provide an object with one property (with the same name as the name of the method argument). The value of the property will be the array.

Reply
Martin Dráb responded on 13 Jun 2018 10:32 AM
Suggested Answer

Sorry, I thought you're talking about a custom service when you commented on my blog about custom services; it seems you actually mean an OData service.

I don't bother writing JSON string manually so I don't know the answer on the top of my head, but let me teach you how you can get it. Download the sample OData application from GitHub. Using the classes to do what you want, execute and observe the JSON sent to AX. And you're done.

Reply

SBX - Two Col Forum

SBX - Migrated JS