Skip to main content
deleted 513 characters in body
Source Link
Peter Csala
  • 10.8k
  • 1
  • 16
  • 36
 {
    "root_id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },          
    "root_mottaker_adresse1": {
        "Path": "InsertDocuments",
        "MainContract": "CreateDocumentParameter"
    },
    "root_mottaker_adresse2": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_web_id_guid": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    }
}
 {
    "root_id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },          
    "root_mottaker_adresse1": {
        "Path": "InsertDocuments",
        "MainContract": "CreateDocumentParameter"
    },
    "root_mottaker_adresse2": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_web_id_guid": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    }
}
{
    "id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },              
    "mottaker": {
        "adresse1": {
            "Path": "InsertDocuments",
            "MainContract": "CreateDocumentParameter"
        },
        "adresse2": {
            "Path": "InsertCases",
            "MainContract": "CreateCaseParameter"
        }
    },
    "web": {
        "id": {
            "guid": {
                "Path": "InsertCases",
                "MainContract": "CreateCaseParameter"
            }
        }
    }
}
{
    "id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },              
    "mottaker": {
        "adresse1": {
            "Path": "InsertDocuments",
            "MainContract": "CreateDocumentParameter"
        },
        "adresse2": {
            "Path": "InsertCases",
            "MainContract": "CreateCaseParameter"
        }
    },
    "web": {
        "id": {
            "guid": {
                "Path": "InsertCases",
                "MainContract": "CreateCaseParameter"
            }
        }
    }
}

if you see the difference the hierarchy is splittedsplit with _(underscore). I want to make it in a more nested way.

  1. root_elementroot_element -> elementelement
  2. root_element1_element2root_element1_element2 -> element1element1 is parent and element2element2 is child.
JObject obj = JObject.Parse(jsonString);
            JObject finalObj = new JObject();
            foreach (var item in obj)
            {
                var keys = item.Key.Replace("root_", "").Split("_").Reverse();
                bool nestedKeyProcessed = false;
                JObject tempObj = new JObject();
                foreach (string key in keys)
                {
                    if (keys.Count() > 1 && !nestedKeyProcessed)
                    {
                        tempObj = CreateJObject(key, item.Value);
                        nestedKeyProcessed = true;
                    }
                    else
                    {
                        if (keys.Count() == 1)
                            finalObj.Add(new JProperty(key, item.Value));
                        else
                            tempObj = CreateJObjectUsingJProperty(key, tempObj);
                    }
                }
                if (keys.Count() > 1)
                    finalObj.Merge(tempObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union });
            }
            string json = JsonConvert.SerializeObject(finalObj);
            JObject CreateJObject(string key, JToken? data)
            {
                JObject obj = new JObject();
                obj.Add(key, data);
                return obj;
            }
            JObject CreateJObjectUsingJProperty(string key, object? data)
            {
                JObject obj = new JObject(new JProperty(key, data));
                return obj;
            }
 {
    "root_id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },          
    "root_mottaker_adresse1": {
        "Path": "InsertDocuments",
        "MainContract": "CreateDocumentParameter"
    },
    "root_mottaker_adresse2": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_web_id_guid": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    }
}
{
    "id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },              
    "mottaker": {
        "adresse1": {
            "Path": "InsertDocuments",
            "MainContract": "CreateDocumentParameter"
        },
        "adresse2": {
            "Path": "InsertCases",
            "MainContract": "CreateCaseParameter"
        }
    },
    "web": {
        "id": {
            "guid": {
                "Path": "InsertCases",
                "MainContract": "CreateCaseParameter"
            }
        }
    }
}

if you see the difference the hierarchy is splitted with _(underscore). I want to make it in a more nested way.

  1. root_element -> element
  2. root_element1_element2 -> element1 is parent and element2 is child.
JObject obj = JObject.Parse(jsonString);
            JObject finalObj = new JObject();
            foreach (var item in obj)
            {
                var keys = item.Key.Replace("root_", "").Split("_").Reverse();
                bool nestedKeyProcessed = false;
                JObject tempObj = new JObject();
                foreach (string key in keys)
                {
                    if (keys.Count() > 1 && !nestedKeyProcessed)
                    {
                        tempObj = CreateJObject(key, item.Value);
                        nestedKeyProcessed = true;
                    }
                    else
                    {
                        if (keys.Count() == 1)
                            finalObj.Add(new JProperty(key, item.Value));
                        else
                            tempObj = CreateJObjectUsingJProperty(key, tempObj);
                    }
                }
                if (keys.Count() > 1)
                    finalObj.Merge(tempObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union });
            }
            string json = JsonConvert.SerializeObject(finalObj);
            JObject CreateJObject(string key, JToken? data)
            {
                JObject obj = new JObject();
                obj.Add(key, data);
                return obj;
            }
            JObject CreateJObjectUsingJProperty(string key, object? data)
            {
                JObject obj = new JObject(new JProperty(key, data));
                return obj;
            }
 {
    "root_id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },          
    "root_mottaker_adresse1": {
        "Path": "InsertDocuments",
        "MainContract": "CreateDocumentParameter"
    },
    "root_mottaker_adresse2": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_web_id_guid": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    }
}
{
    "id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },              
    "mottaker": {
        "adresse1": {
            "Path": "InsertDocuments",
            "MainContract": "CreateDocumentParameter"
        },
        "adresse2": {
            "Path": "InsertCases",
            "MainContract": "CreateCaseParameter"
        }
    },
    "web": {
        "id": {
            "guid": {
                "Path": "InsertCases",
                "MainContract": "CreateCaseParameter"
            }
        }
    }
}

if you see the difference the hierarchy is split with _(underscore). I want to make it in a more nested way.

  1. root_element -> element
  2. root_element1_element2 -> element1 is parent and element2 is child.
JObject obj = JObject.Parse(jsonString);
JObject finalObj = new JObject();
foreach (var item in obj)
{
    var keys = item.Key.Replace("root_", "").Split("_").Reverse();
    bool nestedKeyProcessed = false;
    JObject tempObj = new JObject();
    foreach (string key in keys)
    {
        if (keys.Count() > 1 && !nestedKeyProcessed)
        {
            tempObj = CreateJObject(key, item.Value);
            nestedKeyProcessed = true;
        }
        else
        {
            if (keys.Count() == 1)
                finalObj.Add(new JProperty(key, item.Value));
            else
                tempObj = CreateJObjectUsingJProperty(key, tempObj);
        }
    }
    if (keys.Count() > 1)
        finalObj.Merge(tempObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union });
}
string json = JsonConvert.SerializeObject(finalObj);
JObject CreateJObject(string key, JToken? data)
{
    JObject obj = new JObject();
    obj.Add(key, data);
    return obj;
}
JObject CreateJObjectUsingJProperty(string key, object? data)
{
    JObject obj = new JObject(new JProperty(key, data));
    return obj;
}
Remove TiA- say thanks by voting on answers ; update tags, title to reflect what code achieves per site goals
Source Link

Created structured/nested JSON from unstructured JSON in C# - Optimize

Please review and let me know if weit can do inbe any optimized way..

Thanksoptimized in advance!any way

Created structured/nested JSON from unstructured JSON in C# - Optimize

Please review and let me know if we can do in any optimized way..

Thanks in advance!

Created structured/nested JSON from unstructured JSON

Please review and let me know if it can be any optimized in any way

deleted 1 character in body
Source Link

Please review and let me know if we can do in any optimized way...

Please review and let me know if we can do in any optimized way...

Please review and let me know if we can do in any optimized way..

Source Link
Loading