How to format a collection in json and save using the storage service

0 votes

Using .net to build a server and save data from the server to App42, that clients will then retreive in unity3d.

I pull data from other API with the server ( financial data in that case ), that i put in a collection, and then try to save that on App42.
When i Serialize the collection via newtonsoft.json in C#, i get the followiing json :

[{"id":1,"name":"Bitcoin","symbol":"BTC","slug":"bitcoin","circulating_supply":"17817825","total_supply":"17817825","max_supply":"21000000","date_added":"2013-04-28T00:00:00Z","cmc_rank":1,"price_usd":10671.0584356,"price_btc":1.0,"volume_24h":23185304866.5182,"percent_change_1h":1.57316,"percent_change_24h":-3.85315,"percent_change_7d":-6.54577,"market_cap":190135051770.29456,"last_updated":"2019-07-14T21:13:30Z"},{"id":1027,"name":"Ethereum","symbol":"ETH","slug":"ethereum","circulating_supply":"106899675.5615","total_supply":"106899675.5615","max_supply":null,"date_added":"2015-08-07T00:00:00Z","cmc_rank":2,"price_usd":238.58230671,"price_btc":0.022357885878879574,"volume_24h":8582925524.28892,"percent_change_1h":1.12083,"percent_change_24h":-10.1693,"percent_change_7d":-21.368,"market_cap":25504371182.013287,"last_updated":"2019-07-14T21:13:19Z"}]

Which gives the following error when tryign to save that document :
com.shephertz.app42.paas.sdk.csharp.App42BadParameterException: {"app42Fault":{"httpErrorCode":400,"appErrorCode":2605,"message":"Bad Request","details":"Passed JSON string is not valid."}}
   at com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorAsync.ExecutePost(String url, Dictionary`2 paramsDics, String bodyPayLoad, Dictionary`2 headerParams)
   at com.shephertz.app42.paas.sdk.csharp.storage.StorageService.InsertJSONDocument(String dbName, String collectionName, String json)
   at com.shephertz.app42.paas.sdk.csharp.storage.StorageService.<InsertJSONDocument>c__AnonStorey0.<>m__0()

I am not sure how to serialize that collection in order for it to be "valid" ?

This is how i convert the collection ( using newtonsoft.Json )
string json = JsonConvert.SerializeObject(list_coin);
Storage storage = storageService.InsertJSONDocument(dbName,collectionName,json); 

Any help would be greatly appreciated, as i am planning to use jsons a lot and wish for an easy converter to be used.


 

asked Jul 14 in C# by romain (16 points)

1 Answer

+1 vote
 
Best answer

Hi Romain,

Greetings!!!

Please use the below format for saving the JSON document in the App42 and let us know if the issue still continues. We will be happy to help you.

{ "results":[{"id":1,"name":"Bitcoin","symbol":"BTC","slug":"bitcoin","circulating_supply":"17817825","total_supply":"17817825","max_supply":"21000000","date_added":"2013-04-28T00:00:00Z","cmc_rank":1,"price_usd":10671.0584356,"price_btc":1.0,"volume_24h":23185304866.5182,"percent_change_1h":1.57316,"percent_change_24h":-3.85315,"percent_change_7d":-6.54577,"market_cap":190135051770.29456,"last_updated":"2019-07-14T21:13:30Z"},{"id":1027,"name":"Ethereum","symbol":"ETH","slug":"ethereum","circulating_supply":"106899675.5615","total_supply":"106899675.5615","max_supply":null,"date_added":"2015-08-07T00:00:00Z","cmc_rank":2,"price_usd":238.58230671,"price_btc":0.022357885878879574,"volume_24h":8582925524.28892,"percent_change_1h":1.12083,"percent_change_24h":-10.1693,"percent_change_7d":-21.368,"market_cap":25504371182.013287,"last_updated":"2019-07-14T21:13:19Z"}

] }

 

Regards,

Priyanka Singh

App42 Team

answered Jul 15 by priyanka.singh (166 points)
selected Jul 15 by romain
I have just tried it , i created a new class "Results" that holds the list. Serializing this class gives me :

{
  "results": [
    {
      "id": 1,
      "name": "Bitcoin",
      "symbol": "BTC",
      "slug": "bitcoin",
      "circulating_supply": "17819062",
      "total_supply": "17819062",
      "max_supply": "21000000",
      "date_added": "2013-04-28T00:00:00Z",
      "cmc_rank": 1,
      "price_usd": 10590.6594824,
      "price_btc": 1.0,
      "volume_24h": 25644741665.8057,
      "percent_change_1h": 1.97485,
      "percent_change_24h": -0.345149,
      "percent_change_7d": -11.067,
      "market_cap": 188715617937.77353,
      "last_updated": "2019-07-15T15:03:26Z"
    },
    {
      "id": 1027,
      "name": "Ethereum",
      "symbol": "ETH",
      "slug": "ethereum",
      "circulating_supply": "106909898.5615",
      "total_supply": "106909898.5615",
      "max_supply": null,
      "date_added": "2015-08-07T00:00:00Z",
      "cmc_rank": 2,
      "price_usd": 231.10673994,
      "price_btc": 0.021821751546640022,
      "volume_24h": 10212974767.4644,
      "percent_change_1h": 2.23369,
      "percent_change_24h": -3.18859,
      "percent_change_7d": -25.0298,
      "market_cap": 24707598123.864361,
      "last_updated": "2019-07-15T15:03:21Z"
    }
  ]
}


That gives me the following answer :
com.shephertz.app42.paas.sdk.csharp.App42BadParameterException: {"app42Fault":{"httpErrorCode":400,"appErrorCode":2605,"message":"Bad Request","details":"Passed JSON string '{\"results\":[{\"symbol\":\"BTC\",\"percent_change_1h\":1.34464,\"volume_24h\":2.56089559E10,\"price_usd\":10568.999,\"price_btc\":1,\"id\":1,\"total_supply\":\"17819062\",\"market_cap\":1.88329656E11,\"percent_change_24h\":-0.468007,\"max_supply\":\"21000000\",\"cmc_rank\":1,\"circulating_supply\":\"17819062\",\"date_added\":\"2013-04-28T00:00:00Z\",\"name\":\"Bitcoin\",\"last_updated\":\"2019-07-15T15:05:31Z\",\"slug\":\"bitcoin\",\"percent_change_7d\":-11.2788},{\"symbol\":\"ETH\",\"percent_change_1h\":1.63324,\"volume_24h\":1.02043044E10,\"price_usd\":230.71843,\"price_btc\":0.021829734,\"id\":1027,\"total_supply\":\"106909898.5615\",\"market_cap\":2.46660854E10,\"percent_change_24h\":-3.27978,\"max_supply\":null,\"cmc_rank\":2,\"circulating_supply\":\"106909898.5615\",\"date_added\":\"2015-08-07T00:00:00Z\",\"name\":\"Ethereum\",\"last_updated\":\"2019-07-15T15:05:23Z\",\"slug\":\"ethereum\",\"percent_change_7d\":-25.1646}],\"_$createdAt\":\"2019-07-15T15:06:07.112Z\",\"_$updatedAt\":\"2019-07-15T15:06:07.112Z\"}' is not valid."}}
   at com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorAsync.ExecutePost(String url, Dictionary`2 paramsDics, String bodyPayLoad, Dictionary`2 headerParams)
   at com.shephertz.app42.paas.sdk.csharp.storage.StorageService.InsertJSONDocument(String dbName, String collectionName, String json)
   at com.shephertz.app42.paas.sdk.csharp.storage.StorageService.<InsertJSONDocument>c__AnonStorey0.<>m__0()

EDIT :
I found out the issue : Having a string set to null and getting passed as : str = null
For anyone having the same issue, in order to have newtonsoft.json ignore the null values , need to do :
string json = JsonConvert.SerializeObject(coins, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Hi Romain,

Please share the below detail with us, it will help us to provide better support to you from our end.

Regards,
Priyanka Singh
App42 Team

1. SDK name and version
2. API name
3. Share the code snippet with us
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...