Search code examples
c#jsonjson.net

JSON string with custom dynamically columns. Appending in C#


Have made the following piece of code

        var content = @ " {
          ""
          data "": {
            ""
            id "": 1000000,
            ""
            firstName "": ""
            John "",
            ""
            lastName "": ""
            Doe "",
            ""departments"": [2245],
            ""employeeGroups"": [],
            ""
            custom_186549 "": {
              ""
              name "": ""
              Pension_overenskomst "",
              ""
              type "": ""
              Boolean "",
              ""
              value "": false
            },
            ""
            custom_186550 "": {
              ""
              name "": ""
              Pension 1. arbejdsdag "",
              ""
              type "": ""
              Boolean "",
              ""
              value "": false
            }
          }
        }
        ";

        JObject names = JObject.Parse(content);

        IEnumerable < JToken > CustomColumnsFirst = names.SelectTokens("$.data");

        foreach(JToken item in CustomColumnsFirst) {
          Console.WriteLine(item);
        }

        IEnumerable < JToken > CustomColumnsNames = names.SelectTokens("$.data.*.name");
        IEnumerable < JToken > CustomColumnValues = names.SelectTokens("$.data.*.value");

I'm stuck here, managed to get the names and values into an JToken "Array" but kinda need both a way to combine the CustomColumnNames and the CustomColumnValues, but then also appending them back onto the main data. The content of the "customs" are inditical though but the custom name itself varies. The order of the values inside the customers are random aswell though

I'm quite new to C# coding, so don't know much of the basics yet.

I would need to return a json string with this format without typing in the "custom" column name as that one can differ based on what the API returns:

{
   "data":{
      "id":1000000,
      "firstName":"John",
      "lastName":"Doe",
      "departments": [2245],
      "employeeGroups": []
      "Pension_overenskomst":false,
      "Pension 1. arbejdsdag":false
   }
}

Solution

  • try this

    var data = ((JObject)JObject.Parse(content)["data"]);
    
    JObject items = new JObject();
    foreach (var item in data.Properties())
    if ( item.Name.Contains("custom"))
    //or, thanks to iSR5
     if ( item.Name.StartsWith("custom"))
            if (item.Name.StartsWith("custom"))
            {
              if ((string)item.Value["type"] == "Boolean")
                    items.Add((string)item.Value["name"], item.Value["value"]);
                else if ((string)item.Value["type"] == "Text") items.Add("name", item.Value["value"]);
            }
    else
            items.Add(item.Name, item.Value););
    
    
    JObject newData = new JObject();
    newData.Add("data", items);
    content = newData.ToString();
    //or
     content = newData.ToString(Newtonsoft.Json.Formatting.None);
    

    new content json

    {
      "data": {
        "id ": 1000000,
        "firstName ": "John",
        "lastName ": "Doe",
        "departments": [
          2245
        ],
        "employeeGroups": [],
        "Pension_overenskomst": false,
        "Pension 1.arbejdsdag": false,
        "name": "John Doe"
      }
    }