Converting DynamoDB JSON to Standard JSON with C# - c#

Found this question but it was Java specific, and I need a .net answer:
Converting DynamoDB JSON to Standard JSON with Java
Anyway to do that in C#? Trying to set up a test framework that reads a json file with the dynamodb json as an input for mocked data. I can't seem to figure it out.

You should get pretty JSON from the document object using ToJsonPretty extension method.
Document document = _context.ToDocument(entity); // ignore this line
string jsonString = document.ToJsonPretty(); <--- This is what you need
You would need Amazon.DynamoDBv2.DocumentModel namespace for this message.

Related

Corrupted JSON HTTP response

I am getting a HTTP request for a website and the content type is JSON. However, I am getting a nested JSON that is a unicode and is causing consistency problems.
Here is an example:
{"key1":"value",
"key2":"value",
"key3":{
u'key31':u'value',
u'key32':u'value'}}
This reminds me of python 2.7 troubles but I am not sure how to fix this JSON. I am using C# to parse it. Everything works correctly until I try to access key3.
The content should be a JSON object type but it is considered rather a value or a string.
Thanks for ya help. Is there a way to fix it if it is actually corrupted or am I parsing it wrongly?
You're correct that this json object is not complete / does not have the correct syntax. You're missing a closing '}' character.
How are you parsing your data? Try taking a look at this documentation.
your json object is not in valid formatted it should be like as folllows
{
"key1":"value",
"key2":"value",
"key3":{
" u'key31'":"u'value'",
"u'key32'":"u'value'"
}
}
by any chance do you get this json from python dump? coz Python's unicode literals are not valid JSON, and neither are single quotes

Send JSON snippet as string as part of larger JSON request

I'm interacting with a Web API in which all communication is done with JSON - i.e. we serialise data to send them to them and vice versa.
This works fine, but there is a field called 'CustomData' which is a string type, and we want to use this to store 2-3 more variables. The API requires to send through a JSON string which it then stores and parses internally. However, this JSON is obviously being deserialised on their end as an object and we're getting an error saying it's expecting a string but has got an object.
Is there anyway I can mark this string field as raw JSON, and instruct the server NOT to deserialise it?
I'm using JSON.NET
May be try to encode the JSON string from the sender's side and decode it on your receiving end before adding to your database? That way your overall JSON deserialize won't recognize the data string as JSON.

C# force integer when converting XML to JSON

I am attempting to convert XML into JSON in order to generate a HTTP POST request to an API. I am getting an error because one of the fields is meant to be an integer instead of a string. From what i have read adding "json:Integer="true"" to the node will cause it to become an int, but this doesnt seem to be working for me. Here is the xml and the resulting json. The arrays are working, but the integer is not.
<shipments json:Array="true" xmlns:json="http://james.newtonking.com/projects/json">
<shipment_tracking_number />
<response_shipment_date>2016-10-18T01:00:00.0000000-04:00</response_shipment_date>
<response_shipment_method>UPS Ground</response_shipment_method>
<expected_delivery_date>2016-10-18T01:00:00.0000000-04:00</expected_delivery_date>
<ship_from_zip_code>12345</ship_from_zip_code>
<carrier_pick_up_date>2016-10-18T01:00:00.0000000-04:00</carrier_pick_up_date>
<carrier>UPS</carrier>
<shipment_items json:Array="true">
<shipment_item_id>FF12345K</shipment_item_id>
<alt_shipment_item_id>1234567890</alt_shipment_item_id>
<merchant_sku>B00xxxx</merchant_sku>
<response_shipment_sku_quantity json:Integer="true">1</response_shipment_sku_quantity>
</shipment_items>
</shipments>
string jsonrequest = JsonConvert.SerializeXmlNode(doc,
Newtonsoft.Json.Formatting.None, true);
{"shipments":[
{
"shipment_tracking_number":null,
"response_shipment_date":"2016-10-18T01:00:00.0000000-04:00",
"response_shipment_method":"UPS Ground",
"expected_delivery_date":"2016-10-18T01:00:00.0000000-04:00",
"ship_from_zip_code":"12345",
"carrier_pick_up_date":"2016-10-18T01:00:00.0000000-04:00",
"carrier":"UPS",
"shipment_items":[
{
"shipment_item_id":"FF12345K",
"alt_shipment_item_id":"1234567890",
"merchant_sku":"B00xxxx",
"response_shipment_sku_quantity":"1"
}]
}]
}
I need "response_shipment_sku_quantity":"1" to show up as "response_shipment_sku_quantity":1, but it doesnt seem to be working. I can modify the XML or the code that performs the conversion. I dont mind which as long as this can be done.
You define the attribute wrongly. This is how it should look like.
<response_shipment_sku_quantity json:Type='Integer'>1</response_shipment_sku_quantity>
EDIT:
Newtonsoft.Json XmlNodeConverter
Look methods private void SerializeNode and string dataType = GetDataType(node); they suggest this definition.
Another option is to Deserialize the xml to class with proper types for the properties and after that Serialize it to Json.

Restsharp parsing names not supported by C# Json

Hi I'm making a web api client that returns stuff in json.
I'm using Restsharp that uses newtonsoft.json to deserialize json objects.
The problem is that the server returns an object with a property with #Text as name. Is there a way to make restsharp parse this?
Here is a sample:
image: [
{
#text: http://userserve-ak.last.fm/serve/34s/55125087.png
size: small
}]
All the other properties are being parsed just right the only problem is this one, the property is a string type so no problem in here.
Regards
That JSON isn't valid; you need to put quotes around the #text key (so make it "#text") as well as both values (so "http://...png" and "small").

Best approach to pass hash type data in string form from Rails/Ruby to a C# app?

I need to pass (via string content of a HTTP request/response body) name value pairs of data (like a hash) back from a Ruby on Rails server to a C# client.
Anyone happen to know offhand what would be the best format to do this in? Probably XML I would guess?
tks
PS. So overall the requirement is find a C# method that convert from a String of name/value pairs in JSON format (created by Ruby/Rails) to an existing C# standard name/value pair class/variable (e.g. Array or Dictionary I guess?)
You could to post that data as:
HTTP POST fields (read it with Request.Form)
HTTP POST field with XML (Request.Form, XmlDocument)
HTTP POST field with JSON data (DataContractJsonSerializer)
EDIT: I have this samples:
// Building on Silverlight to send
using (MemoryStream ms = new MemoryStream())
{
new DataContractJsonSerializer(fileList.GetType()).WriteObject(ms, fileList);
// send it
}
// Reading on ASHX page
JobEntry[] files =
new JavaScriptSerializer().Deserialize<Negocio.Cache.JobEntry[]>(
new StreamReader(context.Request.InputStream).ReadToEnd());
Actually, JSON is supported in both, and would certainly do what you require.
Here are links to a Javascript Serializer for C#: Parsing JSON using Json.net
And as long as you require 'json' for Ruby on Rails, you can simply use the "[to_json]"1 method.

Categories