Create json file in C#

I’m trying to modify the code on this json array post: Creating JSON from C#

I don’t think I need an array as I need to generate a json file to sync with SalesForce that generates a file that it’ll list all the open projects (SC01 on OrderHed). I got the code to pull the data but struggling to get it into json. I’m close just… stuck.

Error is:

Inner Exception

Can not add Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JObject.

dynamic jsonData; 
dynamic projRow;

var tt = ttCustomer.FirstOrDefault();
if(tt != null )
{
 jsonData = new JObject();

  var list = (from oh in Db.OrderHed.With(LockHint.NoLock) 
              where oh.Company == Session.CompanyID  && oh.BTCustNum == tt.CustNum && oh.OpenOrder && oh.ShortChar01 != ""
              select new {oh.ShortChar01} ).ToList();

  foreach(var result in list)
  {    
//Same error with this code too 
//    projRow = new JObject(
//      new JProperty("ProjID", result.ShortChar01),
//      new JProperty("CreditLimit", tt.CreditLimit),
//      new JProperty("CustType_c", tt["CustType_c"])
//    );
//    jsonData.Add(projRow);

    projRow = new JObject();
    projRow.ProjID = result.ShortChar01;
    projRow.CreditLimit = tt.CreditLimit;
    projRow.CustType_c = tt["CustType_c"];
    jsonData.Add(projRow);

  } 
}

Ok so I did need an array!

image

dynamic jsonData; 
dynamic projRow;

var tt = ttCustomer.FirstOrDefault();
if(tt != null )
{
 jsonData = new JObject();
 jsonData.projRow = new JArray() as dynamic;

  var list = (from oh in Db.OrderHed.With(LockHint.NoLock)
              where oh.Company == Session.CompanyID && oh.BTCustNum == tt.CustNum  && oh.OpenOrder && oh.ShortChar01 != ""
              select new {oh.ShortChar01} ).ToList();

  foreach(var result in list)
  {    
    projRow = new JObject();
    projRow.ProjID = result.ShortChar01;
    projRow.CreditLimit = tt.CreditLimit;
    projRow.CustType_c = tt["CustType_c"];
    jsonData.projRow.Add(projRow);
  } 

    string jsonDataDebug = JsonConvert.SerializeObject(jsonData);     
    Debug = jsonDataDebug;
3 Likes