2

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
   }
}
3
  • 1
    Is it an option to change your json so that the custom items are an array of objects instead of individual objects with unpredictable names? Commented Mar 5, 2022 at 13:40
  • @crowcoder, can't change anything, this is how i receive the response from the API unfortunately. The content of the "customs" are inditical though but the custom name itself varies. Commented Mar 5, 2022 at 15:22
  • That's an unfortunate misuse of json. At a glance it looks like Serge has shown you what to do. Commented Mar 5, 2022 at 15:43

1 Answer 1

2

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"
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

item.Name.Contains("custom") would be better if changed to StartWith
Serge solution looks like it's doing the trick!
@iSR5 It's StartsWith right? Otherwise it doesn't seem to be a function
Do you know how i can return the JObject newData back into a jsonstring? Need to upload the result in a flat .json file
@AlexanderMilland content = newData.ToString(); I updated my answer
|

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.