3

I have a JSON string coming from a file with multiple JSON objects that I need to deserialise into one merged C# object.

File1.json

{
   "manage_employees_section_title": 
    {
        "value": "Manage employees",
        "description": "The mange employees section title"
    }
}
{
   "manage_operations_section_title": 
    {
        "value": "Manage operations",
        "description": "The mange operations section title"
    }
}

Even though there are multiple JSON objects in the file, I would really like to have back from deserialisation (or some other way) one merged C# object like it came from a string like this:

{
   "manage_employees_section_title": 
    {
        "value": "Manage employees",
        "description": "The mange employees section title"
    },
   "manage_operations_section_title": 
    {
        "value": "Manage operations",
        "description": "The mange operations section title"
    }
}

Is this possible with JSON.NET or any other tool ?

Many thanks in advance guys..

1
  • @dav_i I was just thinking exactly the same thing, thanks very much. Commented Mar 12, 2015 at 16:35

2 Answers 2

2

The first code block isn't valid JSON. If you want JSON libraries to deal with your input you'll first need to convert it into valid JSON.

If you're input is always going to look like that, you could use a regex to find the }\r\n\{ and replace it with a comma, which will then produce your second example:

var output = Regex.Replace(input, "\r\n}\r\n{", ",");

With the input of the first example you provided, this now produces the second example as output, which is valid JSON and can be deserialized appropriately.

Sign up to request clarification or add additional context in comments.

Comments

0

If a combined XmlDocument is good enough, then you can:

        string json1 = "{ \"manage_employees_section_title\": {\"value\": \"Manage employees\",\"description\": \"The mange employees section title\"}}";
        string json2 = "{ \"manage_operations_section_title\": {\"value\": \"Manage operations\",\"description\": \"The mange operations section title\"}}";

        XmlDocument doc = new XmlDocument();
        var root = doc.CreateElement("element", "root", "");
        doc.AppendChild(root);

        var xmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json1);
        var xmlNode2 = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json2);

        foreach (XmlNode node in xmlNode.ChildNodes)
        {
            XmlNode imported = doc.ImportNode(node, true);
            doc.DocumentElement.AppendChild(imported);
        }

        foreach (XmlNode node in xmlNode2.ChildNodes)
        {
            XmlNode imported = doc.ImportNode(node, true);
            doc.DocumentElement.AppendChild(imported);
        }

which yields:

<?xml version="1.0" ?>
<root>
    <manage_employees_section_title>
        <value>Manage employees</value>
        <description>The mange employees section title</description>
    </manage_employees_section_title>
    <manage_operations_section_title>
        <value>Manage operations</value>
        <description>The mange operations section title</description>
    </manage_operations_section_title>
</root>

Comments

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.