1

I am creating a Dynamic JSON using ExpandoObject and IDictionary.

My Sample Code is as below :

DataTable dt_MappedColumns = new DataTable();
    dt_MappedColumns.Columns.Add("Sr.No");
    dt_MappedColumns.Columns.Add("TColumnName");
    dt_MappedColumns.Columns.Add("AColumnName");
    dt_MappedColumns.Rows.Add("1", "Apple", "Lion");
    dt_MappedColumns.Rows.Add("2", "Orange", "Tiger");
    dt_MappedColumns.Rows.Add("3", "Mango", "Fox");
    dt_MappedColumns.Rows.Add("4", "Orange", "Wolf");

    var dictionary1 = new Dictionary<string, object>();
    foreach (DataRow dr in dt_MappedColumns.Rows)
    {
        string key = dr["TColumnName"].ToString();
        string value = dr["AColumnName"].ToString();

        if (!dictionary1.ContainsKey(key))
        {
            // key does not already exist, so add it
            dictionary1.Add(key, value);
        }
        else
        {
            // key exists, get the existing value
            object existingValue = dictionary1[key];

            if (existingValue is string)
            {
                // replace the existing string value with a list
                dictionary1[key] = new List<string> { (string)existingValue, value }; 
            }
            else
            {
                // the existing value is a list, so just add the new value to it
                ((List<string>)existingValue).Add(value);
            }
        }
    }

    string Manjson = JsonConvert.SerializeObject(dictionary1);

The JSON String so Obtanined is as below with Escape character {\}:

"{\"Apple\":\"Lion\",\"Orange\":[\"Tiger\",\"Wolf\"],\"Mango\":\"Fox\"}".

I wish to remove the Escape character {\} and desired JSON would be like below :

"{"Apple":"Lion", "Orange":["Tiger","Wolf"], "Mango":"Fox"}"

**** EDIT ****

Interestingly, if I print the JSON in console, it is without Escape Character, but when I insert into Oracle DB Table it inserts with Escape Character.

Using Visual Studio 2010 Professional with Oracle 11g and PL/SQL 11.2

What would be the solution for this :

Any help is appreciated :)

Mufaddal
  • 558
  • 5
  • 24
  • Are you saying that the JSON actually contains the slash character? If you are viewing the string in the debugger you would expect to see these slashes – Martin Dec 07 '18 at 13:16
  • What are using to verify the slash is actually in the string. It's more likely that it is only being escaped by whatever debugging tool you are using. – phuzi Dec 07 '18 at 13:16
  • Possible duplicate of [Remove only \ from JSON](https://stackoverflow.com/questions/33824802/remove-only-from-json) – phuzi Dec 07 '18 at 13:18

1 Answers1

1

I resolved the issue by converting it into a JObject and then using it as follows:

using Newtonsoft.Json.Linq;

var Varjson = JObject.Parse(Manjson);

where Manjson is my JSON string with escape characters ('/').

The Object Varjson will contain the deserialized Json without escape character as below:

"{"Apple":"Lion", "Orange":["Tiger","Wolf"], "Mango":"Fox"}"

:)

Community
  • 1
  • 1
Mufaddal
  • 558
  • 5
  • 24