0

I have an XML file from which dynamically creates a JSON. The last entry in each row should get a value when the JSON is finished.

However when the JSON is finished only the last entry from the last row has a value. I guess its overwriting since I need to recreate every entry for the right JSON structure.

I split the Group of each row and each point is a JObject except the one with "[0]", thats a JArray.

Has anyone an idea how I can avoid overwriting my JObjects? Thanks.

My XML My JSON

private static void GenerateElement(JObject parent, string path, string value)
    {
        var parts = path.Split('.');

        foreach (var item in parts)
        {
            var partsCount = parts.Count();

            var currentElement = item;

            if (partsCount > 1)
            {
                path = path.Remove(0, item.Length);
                if (path.StartsWith("."))
                {
                    path = path.Remove(0, 1);
                }
                else if (path.EndsWith("."))
                {
                    path = path.Remove(path.Length - 1, 1);
                }
                parts = path.Split('.');
            }
            var nextElementPath = path;

            var elementArrayName = currentElement;

            if (currentElement.IndexOf("[0]") >= 0)
            {
                elementArrayName = currentElement.Substring(0, currentElement.IndexOf("[0]"));
            }
            else
            {
                parent[currentElement] = new JObject();
            }
            if (partsCount > 1)
            {
                if (parent[nextElementPath] == null)
                {
                    if (parent[elementArrayName] == null)
                    {
                        parent[elementArrayName] = new JArray();
                        (parent[elementArrayName] as JArray).Add(new JObject());
                    }
                }
                if (parent[elementArrayName] is JArray)
                {
                    parent = parent[elementArrayName][0] as JObject;
                }
                else
                {
                    parent = parent[currentElement] as JObject;
                }
            }
            else
            {
                parent[currentElement] = value;
            }
        }
        
    }
campi
  • 11
  • 1
  • 5
  • Might you please [edit] your question to include your JSON and XML as **text** rather than as a screenshot? It's policy here not to to use images for textual data, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. A compilable and runnable [mcve] with full code and data which we can run and debug would increase your chances of getting an answer, for why see [ask]. – dbc Sep 01 '21 at 14:51
  • If what you are trying to do is to recursively populate a `JObject` from a JSONPath and a value, then check to see whether [this answer](https://stackoverflow.com/a/40858665) by [Danny Su](https://stackoverflow.com/users/1311686/danny-su) to [How to unflatten flattened json in C#](https://stackoverflow.com/q/40541842) does what you want. – dbc Sep 01 '21 at 15:30

0 Answers0