Skip to content

Instantly share code, notes, and snippets.

@rcurlette
Last active January 24, 2018 15:26
Show Gist options
  • Save rcurlette/308bac15f4f449d78df79d5f7b25c3f9 to your computer and use it in GitHub Desktop.
Save rcurlette/308bac15f4f449d78df79d5f7b25c3f9 to your computer and use it in GitHub Desktop.
Json.Net Code Sample
// get our JSON objects
dynamic jsonObject = JObject.Parse(jsonContent);
dynamic metaPage = jsonObject.MetadataFields;
// check if property exists in the JSON
if (metaPage.Property("contact") == null)
{
dynamic contact = GetContact(sgMetadataFields);
if(contact != null)
{
// Add a new JSON node into the metaPage node
metaPage.AddFirst(new JProperty("contact", contact));
}
}
// Replace the existing empty MetadataFields node with the new popluated one
jsonObject.MetadataFields.Replace(metaPage);
// We also need to produce some interestingly structured JSON that appears like this:
//"right1": {
// "EmbeddedValues": [
// {
// "size": {
// "Name": "size",
// "Values": [ "300x250" ]
// }
// }
// ],
// "EmbeddedSchema": {
// "RootElementName": "Content",
// "Id": "tcm:11-702-8",
// "Title": "Ad Metadata"
// },
// "FieldType": 0,
// "Name": "right1"
// },
dynamic rightJsonNode = new JObject();
string sizeRightField = "160x160";
if (sizeRightField != "")
{
dynamic aSize = new JObject();
aSize.size = new JObject();
aSize.size.Name = "size";
List<string> sizeEmbeddedFieldValue = new List<string>()
{
sizeRightField //"160x600" //sizeRight1
};
aSize.size.Values = JArray.FromObject(sizeEmbeddedFieldValue);
// container for embedded field
List<dynamic> valuesEmbedded = new List<dynamic>()
{
aSize
};
dynamic EmbeddedValues = JArray.FromObject(valuesEmbedded);
rightJsonNode.EmbeddedValues = EmbeddedValues;
}
// Embedded schema Definition
dynamic embeddedSchemaFieldDef = new JObject();
embeddedSchemaFieldDef.RootElementName = "Content";
embeddedSchemaFieldDef.Id = "tcm:11-1233-8";
embeddedSchemaFieldDef.Title = "Ad Metadata";
rightJsonNode.EmbeddedSchema = embeddedSchemaFieldDef;
// Fieldtype
rightJsonNode.FieldType = 4; //embedded
// Name
rightJsonNode.Name = fieldName;
metaPage.AddFirst(new JProperty("right1", rightJsonNode));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment