How do I get the outgoing JSON from a CloudKit request? - c#

I'm debugging the JSON for a C# app that POSTs to CloudKit using server keys.
According to the docs, the JSON I need to use to create/modify a record looks like this:
{
"operationType" : "create",
"record" : {
"recordType" : "Artist",
"fields" : {
"firstName" : {"value" : "Mei"},
"lastName" : {"value" : "Chen"}
}
"recordName" : "Mei Chen"
},
}
When I go to JSON2Csharp, I get the error below. I need to determine if the documentation is invalid, or if the server actually wants this JSON as-is.
Question
How can I obtain the JSON used in the apple CloudKit framework when it's sent to the CK server?

Your JSON doesn't appear to be formatted properly.
Please see if this fits your needs, as I believe it to be correctly formatted and structured to what you are working with:
{
"operationType" : "create",
"record" :
{
"recordType" : "Artist",
"fields" :
{
"firstName" : {"value" : "Mei"},
"lastName" : {"value" : "Chen"}
},
"recordName" : "Mei Chen"
}
}

Related

C# MongoDbDriver - Unset, Cannot make it unset field in database

I'm trying to find the way to make this work.
Documentation of the MongoDbDriver is pretty poor, and just a couple of "solutions" found by google.
I need to remove the field from the database through C# migration script.
var filter = Builders<Person>.Filter.Eq(person => person.Id, personId);
var update = Builders<Person>.Update.Unset(person => person.Address.PostalCode);
collection.UpdateOne(filter, update);
This is what I found as a solution on the net, but this doesn't work. Field is still in the database.
What am I missing?
Just checked, your above query works as expected, see results below before and after:
before updating:
MongoDB Enterprise replset:PRIMARY> db.p.find({})
{ "_id" : ObjectId("616ace670b6d6703634faf70"), "Address" : { "PostalCode" : "200" } }
{ "_id" : ObjectId("616ace660b6d6703634faf6f"), "Address" : { "PostalCode" : "100" } }
after updating by personId = ObjectId("616ace660b6d6703634faf6f")
MongoDB Enterprise replset:PRIMARY> db.p.find({})
{ "_id" : ObjectId("616ace670b6d6703634faf70"), "Address" : { "PostalCode" : "200" } }
{ "_id" : ObjectId("616ace660b6d6703634faf6f"), "Address" : { } }

aws VS2017 serverless app syntax in serverless.template to add filter to s3 notification

I have created a VS2017 C# app using the AWS Serverless Application template with the "Simple S3 Function" blueprint. The CloudFormation serverless.template file contains a spec for my handler function with an event spec to respond to "s3.ObjectCreated:*" events. I am trying to add a filter specification to that event spec to only respond to events with the "Source/" prefix. Here is my code:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Transform" : "AWS::Serverless-2016-10-31",
"Description" : "Template that creates a S3 bucket and a Lambda function that will be invoked when new objects are upload to the bucket.",
"Parameters" : {
"BucketName" : {
"Type" : "String",
"Description" : "Name of S3 bucket to be created. The Lambda function will be invoked when new objects are upload to the bucket. If left blank a name will be generated.",
"MinLength" : "0"
}
},
"Conditions" : {
"BucketNameGenerated" : {"Fn::Equals" : [{"Ref" : "BucketName"}, ""]}
},
"Resources" : {
"Bucket" : {
"Type" : "AWS::S3::Bucket",
"Properties" : {
"BucketName" : { "Fn::If" : ["BucketNameGenerated", {"Ref" : "AWS::NoValue" }, { "Ref" : "BucketName" } ] }
}
},
"S3Function" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "DCATInventory::DCATInventory.Function::FunctionHandler",
"Runtime": "dotnetcore2.0",
"CodeUri": "",
"Description": "Default function",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess", "AmazonRekognitionReadOnlyAccess" ],
"Events": {
"NewImagesBucket" : {
"Type" : "S3",
"Properties" : {
"Bucket" : { "Ref" : "Bucket" },
"Events" : [
"s3:ObjectCreated:*"
],
"Filter" : {
"S3Key" : {
"Rules" : [{
"Name" : "prefix",
"Value": "Source/"
}]
}
}
}
}
}
}
}
},
"Outputs" : {
"Bucket" : {
"Value" : {"Ref":"Bucket"},
"Description" : "Bucket that will invoke the lambda function when new objects are created."
}
}
}
This is the default code generated by the template with only the Filter spec added to the event properties. I am receiving an error stating "Rules key is invalid for this object" on line 48. I have read the documentation and googled this and this seems to be the correct syntax. Did I specify something wrong here? Thanks in advance.
It turns out my syntax shown above is correct even though Visual Studio is reporting an error. I decided to try publishing the app to AWS even with this error. I expected I would get an error from CloudFront, but it published successfully. The S3 Event did publish and contains my filter rule with the "Source/" prefix.

Deleting a mongodb subdocument inside another that is inside another?

I have read other posts similar to this one, but since I am new to mongodb and the depth of the subdocument that I have is more then already addressed, I have to ask this question here. My document is as follows:
{
"_id" : "57bae0ad7bbba417fcaec4ca",
"spUserProfile" : null,
"spLinkedBusinesses" : [
{
"_id" : "57bae0ad7bbba417fcaec4c9",
"businessDocuments" : [
{
"_id" : "57bae0fb7bbba417fcaec4cc",
"documentPath" : "/docs/doc1.pdf",
"documentName" : "Doc Hey",
"documentType" : "DPF",
"documentUploadDateTime" : ISODate("2016-08-22T11:24:43.061Z")
},
{
"_id" : "57bae0fd7bbba417fcaec4cd",
"documentPath" : "/docs/doc_mute.pdf",
"documentName" : "Agreements IMP",
"documentType" : "PDF",
"documentUploadDateTime" : ISODate("2016-08-22T11:24:45.229Z")
},
{
"_id" : "57bae0ff7bbba417fcaec4ce",
"documentPath" : "/docs/accounts1.xls",
"documentName" : "Expenses 1",
"documentType" : "XLS",
"documentUploadDateTime" : ISODate("2016-08-22T11:24:47.066Z")
}
]
}
]
}
I have to delete the businessDocuments with id "57bae0fd7bbba417fcaec4cd" (using c# mongodb driver code), the collection is called RefUsers.
PS: I cant use the legacy C# mongodb drivers, so no use of the Query class.
thanks.
Have a look into $pull to remove documents from an embedded array
Pull MongoDB documentation
ElemMatch will also be useful to you here if you are wanting to identify items within embedded arrays
ElemMatch MongoDB documentation

Assign JSON value to property in C#

From an API I receive a JSON-object that looks like this:
{
"wind" : {
"speed" : 7.31,
"deg" : 187.002
},
"rain" : {
"3h" : 0
},
"clouds" : {
"all" : 92
},
"coord" : {
"lon" : 139,
"lat" : 35
},
"dt" : 1369824698,
"id" : 1851632,
"cod" : 200,
"weather" : [
{
"id" : 804,
"main" : "clouds",
"icon" : "04n",
"description" : "overcast clouds"
}
],
"main" : {
"humidity" : 89,
"temp_max" : 292.04,
"temp_min" : 287.04,
"temp" : 289.5,
"pressure" : 1013
},
"sys" : {
"country" : "JP",
"sunrise" : 1369769524,
"sunset" : 1369821049
},
"name" : "Shuzenji"
}
I would like to assign two of these values to my class:
public class Weather {
public string Name { get; set; }
public string Temp { get; set; }
}
The name I can assign like this:
weather.Name = TheJSON.name.ToString();
But the temp is trickier, because it's nested inside the "main"-array. I´ve seen many examples on how to do this in Javascript but not so much in C#. Thanks!
Main is not an array. It is an object, so
TheJSON.main.temp.ToString()
The easiest way to work with JSON data is to deserialize them as C# objects and directly use them in your application. You can use a tool like JSON C# Class Generator to automatically generate the C# class from the JSON data. Once you have your C# classes generated, you can deserialize the JSON string using the JsonConvert.DeserializeObject(jsonText); The generated code requires Newtonsoft Json.NET which you can easily add as a NuGet package.
If you save your JSON content in D:\test.json, you can use the following code to access the values using the C# objects generated. The example below is to just give you an idea on the usage.
var json = File.ReadAllText(#"D:\test.json");
var weather = JsonConvert.DeserializeObject<Weather>(json);
Console.WriteLine(weather.Name);
Console.WriteLine(weather.Sys.Country);

Mongo deepFind?

I Have a mongoDB that contains data in unknown structure, and I want to find all the documents that has a specific BsonElement. Can I do it without the knowledge about the documents structure?
For example, if the documents look like this (and I don't know that) can I find all the documents that has "DBType":"MSSQL"?
There is somting like collection.deepFind(QueryDocument);?
{
"_id" : ObjectId("528fe1602feaa3231c784d92"),
"CurrentJobs" : {
"_t" : "sqQueryJob",
"IsDone" : false,
"ExecuteTime" : ISODate("2013-11-22T22:57:35.733Z"),
"Result" : { },
"DB" : {
"DBType" : "MSSQL",
"Name" : "Data",
"Host" : ".",
"ServiceName" : "DataBase",
"Port" : "1521",
},
"QueryResult" : null,
"Query" : "."
}
}
Thanks!

Categories