0

I'm having trouble transforming a JSON response from an API.

Basically their API returns me a JSON objects with this format:

{
   "data":{
      "id":1000000,
      "firstName":"John",
      "lastName":"Doe",
      "custom_186549":{
         "name":"Pension_overenskomst",
         "type":"Boolean",
         "value":false
      },
      "custom_186550":{
         "name":"Pension 1. arbejdsdag",
         "type":"Boolean",
         "value":false
      }
   }
}

Problem is that different employees will have different custom_XXXX names while they contain similar information.

In the picture you see custom_186549 contains information about "pension_overenskomst". But for another employee this might be inside custom_175134. I need to unify their format and get the sub fields of "pension_overenskomst" as the JSON object name and get rid of the custom elements.

I would like it to just say

{
   "data":{
      "id":1000000,
      "firstName":"John",
      "lastName":"Doe",
      "Pension_overenskomst":false,
      "Pension 1. arbejdsdag":false
   }
}

Regardless of what "custom" element it's inside.

Update: Managed to get the elements into an array. Now I just need to find a way to append them back into the JSON string.

var response = await httpClient.SendAsync(request);

var content = await response.Content.ReadAsStringAsync();

var responseObj = JsonConvert.DeserializeObject<dynamic>(content).data;

JObject names = JObject.Parse(content);

IEnumerable<JToken> CustomColumnsNames = names.SelectTokens("$.data.*.name");
IEnumerable<JToken> CustomColumnValues = names.SelectTokens("$.data.*.value");
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Can you show the input and expected output json? Can you show some starting code? – vernou Mar 02 '22 at 12:10
  • @vernou is right. if you share the input at least, we may be able to suggest the solution. – Levent Üncü Mar 02 '22 at 14:07
  • Current output: { "data": { "id": 1000000, "firstName": "John", "lastName": "Doe", "custom_186549": { "name": "Pension_overenskomst", "type": "Boolean", "value": false }, "custom_186550": { "name": "Pension 1. arbejdsdag", "type": "Boolean", "value": false } } } – Alexander Milland Mar 02 '22 at 14:24
  • Expected output: { "data": { "id": 1000000, "firstName": "John", "lastName": "Doe", "Pension_overenskomst": false "Pension 1. arbejdsdag": false } } – Alexander Milland Mar 02 '22 at 14:24
  • The custom "element" have multiple diffrent suffixes though. I basically get return of 1000 different json strings which i want to append and have the same format. – Alexander Milland Mar 02 '22 at 14:26
  • Can you also add a minimum input json? – vernou Mar 02 '22 at 14:29
  • The current output is the input, not transforming anything at the moment. I forgot a comma in the expected output though : { "data": { "id": 1000000, "firstName": "John", "lastName": "Doe", "Pension_overenskomst": false, "Pension 1. arbejdsdag": false } } – Alexander Milland Mar 02 '22 at 14:52
  • You can use JSONPath combined with newtonsoft JSON's SelectToken function. For more information please check https://goessner.net/articles/JsonPath/index.html#e2 and for testing you can use http://jsonpath.com/ – Levent Üncü Mar 02 '22 at 15:30
  • 1
    You should check answer in this thread https://stackoverflow.com/questions/47043028/serialize-deserialize-dynamic-property-name-using-json-net Should do the trick. – atomicicebreaker Mar 03 '22 at 14:02

0 Answers0