1. Wilko Lindemann
  2. .NET DataStore
  3. Tuesday, 9 February 2021 07:57 AM UTC

Hi,

I'm looking for a way to generate data in a specific JSON format to sent it to a unknown web server. 

I have two tables (table1 with column1, column2, column3 and table2 with column2, column4). The table1 has a foreign key to table2 (column2). 

 The JSON should look like:

{
     "column1": "xxx",
     "table2": {
        "column2": 1
     },
     "column3": "xxx"
}

 

So the requirements are:

1. the data of the foreign key must be in a extra segment

2. only the primary key of table2 should be part of the extra segment

3. NULLvalues should be ignored

 

Suggestions, guidance, and samples are welcome and appreciated!

 

Thanks and regards, 

Wilko Lindemann

 

 

Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 17 February 2021 16:32 PM UTC
  2. .NET DataStore
  3. # 1

Hi Wilko,

You can also try to use DataPacker to generate data in a specific JSON format. Then you can focus on the data element and write less code.

https://docs.appeon.com/snapobjects/3.0/api_reference/SnapObjects.Data/IDataPacker/IDataPacker.html

Regards,

Logan

Comment
  1. Wilko Lindemann
  2. Thursday, 25 February 2021 16:25 PM UTC
Hi Logan,



Thank you for your answer.



I've tried out the DataPacker and works fine except the part of ignoring NULL values.

Is there an option for the DataPacker that I just haven't seen?



Regards,



Wilko

  1. Helpful
  1. Logan Liu @Appeon
  2. Thursday, 25 February 2021 17:17 PM UTC
There is no option of ignoring NULL values.
  1. Helpful
There are no comments made yet.
Marco Meoni Accepted Answer Pending Moderation
  1. Tuesday, 9 February 2021 18:54 PM UTC
  2. .NET DataStore
  3. # 2

Hi Wilko,

JSONgenerator object should have all you need, have a look at the multiple "add" methods:

https://docs.appeon.com/pb2017r2/objects_and_controls/ch02s40.html

If any, what are parts of your JSON that you think might not beed generated?

Best,

.m

Comment
  1. Chris Pollach @Appeon
  2. Tuesday, 9 February 2021 20:18 PM UTC
Hi Marco;

FYI: Wilko's post refers to a .NET DataStore .. so I "think" that he is referring to a SNAP C# object not PB. Could be wrong here though. Just my guess.

Regards ... Chris
  1. Helpful
  1. Marco Meoni
  2. Tuesday, 9 February 2021 21:07 PM UTC
oops, didnt see the tag. :-)

Then I would have a look at json.net from newtonsoft.

A .NET datastore is an Enumerable, and all IEnumerable types can be serialized as json array.

Best,

.m
  1. Helpful
  1. Wilko Lindemann
  2. Thursday, 11 February 2021 15:14 PM UTC
Hi Marco,



thanks for your answer.



I've tried 2 things from newtonsoft. "SerializeObject" from "JsonConvert" and the "JsonWriter".



First to the "JsonConvert". It works fine to eliminate NULL values, but now the upcomming problem is that I'm just able to serialize 1 datastore with the following result:



{

"column1": "xxx",

"column2": 1,

"column3": "xxx"

}



1. So how do I can force an extra section into the created json?

2. Do I have to link the secound datastore for table2 with the first one?

3. Is there an equivalent to the modelbuilder in entity framework for .NET Datastores?



Now my question to the JsonWriter:

1. Do I realy have to write "WritePropertyName" and "WriteValue" for every coulmn? Isn't the a more elegant method?

2. Does exits an option for ignoring NULL values similar to the "NullValueHandling" of "JsonSerializerSettings" for the "JsonConvert"? Or do I have to check every value in an if-clause before writing the property?



My Code looks like this:



var dsStore = new DataStore<Ds_Table1>(context);

dsStore.Retrieve();



StringBuilder sb = new StringBuilder();

StringWriter sw = new StringWriter(sb);



using (JsonWriter writer = new JsonTextWriter(sw))

{

writer.Formatting = Formatting.Indented;

for (int i=0;i<dsStore.RowCount;i++)

{

writer.WriteStartObject();

writer.WritePropertyName("column1");

writer.WriteValue(dsStore.GetItem<int>(i,"column1",DwBuffer.Primary));

// extra section for column2

writer.WritePropertyName("table2");

writer.WriteStartObject();

writer.WritePropertyName("column2");

writer.WriteValue(dsStore.GetItem<int>(i,"column2",DwBuffer.Primary));

writer.WriteEnd();

writer.WritePropertyName("column3");

writer.WriteValue(dsStore.GetItem<int>(i,"column3",DwBuffer.Primary));

//... some more columns

writer.WriteEndObject();

}

}



Thanks and regards,



Wilko Lindemann
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.