Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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