1

How can i store a JSON object in an attribute of JSON object, i mean the value of an attribute of JSON object is another JSON object. I have a DataTable dt, i read data from DataBase and store it in this DataTable.

using (var da = new SqlDataAdapter(command))
{
         command.CommandType = CommandType.StoredProcedure;
         da.Fill(dt);
}

Now, i add a column to this DataTable

dt.Columns.Add("attributes");

Now, i create a JObject and store its value in the "attributes" column for each row of DataTable

dynamic attributeValue = new JObject();
attributeValue.type = "Stage_FF_Hot_Alerts__c";

foreach (DataRow d in dt.Rows)
{
       d["attributes"] = attributeValue;
}

Now, i Serialize this DataTable

string JSONresult = JsonConvert.SerializeObject(dt);

The result which i get is

{
  "attributes" : "{"type" : "Stage_FF_Hot_Alerts__c"}",
  "Address__c"   : "Street",
  "AgentID__c"   : "123456",
  "Alert_Status__c"  : "Closed",
  "BusinessUnit__c"  : "INFINITI",
  "Case_Type__c" : "INFINITI Service",
  "City__c"      : "City",
  "ContactId__c"     : "10951",
  "DayTimePhone__c"     : "123456789",
  "DealerCode__c"    : "72067",
  "DealerName__c"    : "Infiniti Of Kansas City",
  "EmailAddress__c"  : "[email protected]",
  "EveningPhone__c"  : "123456789",
  "FirstName__c" : "CustomerFirstName",
  "HotAlertType__c"  : "Hot Alert",
  "LastName__c"  : "CustomerSurname",
  "NPS_Score_1__c"   : "0",
  "V01_Alert_Trigger__c"     : "Which of the following best describes your overall service experience?",
  "Field_Open_Date__c"   : "2018-08-05"
}

while the result which i want is

{
  "attributes" : {"type" : "Stage_FF_Hot_Alerts__c"},
  "Address__c"   : "Street",
  "AgentID__c"   : "123456",
  "Alert_Status__c"  : "Closed",
  "BusinessUnit__c"  : "INFINITI",
  "Case_Type__c" : "INFINITI Service",
  "City__c"      : "City",
  "ContactId__c"     : "10951",
  "DayTimePhone__c"     : "123456789",
  "DealerCode__c"    : "72067",
  "DealerName__c"    : "Infiniti Of Kansas City",
  "EmailAddress__c"  : "[email protected]",
  "EveningPhone__c"  : "123456789",
  "FirstName__c" : "CustomerFirstName",
  "HotAlertType__c"  : "Hot Alert",
  "LastName__c"  : "CustomerSurname",
  "NPS_Score_1__c"   : "0",
  "V01_Alert_Trigger__c"     : "Which of the following best describes your overall service experience?",
  "Field_Open_Date__c"   : "2018-08-05"
}
1

1 Answer 1

1

The following line in your sample code is creating a column in the DataTable without specifying the data type, so the type defaults to string.

dt.Columns.Add("attributes");

Try using an overload that specifies the type you want, e.g.:

dt.Columns.Add("attributes", typeof(object));

or maybe:

dt.Columns.Add("attributes", typeof(JObject));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.