0

UPDATE

All I want is to remove backslashes from following string:

"[{\"type\":\"text\",\"label\":\"Name\",\"className\":\"form-control\",\"name\":\"text-1605888860074\"},{\"type\":\"radio-group\",\"label\":\"Gender\",\"inline\":true,\"name\":\"radio-group-1605892686644\",\"values\":[{\"label\":\"Male\",\"value\":\"Male\",\"selected\":false},{\"label\":\"Female\",\"value\":\"Female\",\"selected\":false}]},{\"type\":\"date\",\"label\":\"D.O.B.\",\"className\":\"form-control\",\"name\":\"date-1605892732455\"},{\"type\":\"checkbox-group\",\"label\":\"Disabled\",\"inline\":true,\"name\":\"checkbox-group-1605892754084\",\"values\":[{\"label\":\"Yes\",\"value\":\"Yes\",\"selected\":true},{\"label\":\"No\",\"value\":\"No\",\"selected\":false}]}]"

Required output:

"[{"type":"text","label":"Name","className":"form-control","name":"text-1605888860074"},{"type":"radio-group","label":"Gender","inline":true,"name":"radio-group-1605892686644","values":[{"label":"Male","value":"Male","selected":false},{"label":"Female","value":"Female","selected":false}]},{"type":"date","label":"D.O.B.","className":"form-control","name":"date-1605892732455"},{"type":"checkbox-group","label":"Disabled","inline":true,"name":"checkbox-group-1605892754084","values":[{"label":"Yes","value":"Yes","selected":true},{"label":"No","value":"No","selected":false}]}]"

ORIGINAL

I'm using formbuilder to generate forms schema and saving in MySQL database using C# Asp.Net Web Api (.Net Core 3.1). Formbuilder sends form schema to server as json string with escape sequences \n \r \" i'm using schema.Replace("\n","").Replace("\r","").Replace(" ", "") and it removes \n \r and blank spaces as expected but \" is problematic. I want to replace \" with " to make valid JSON Array. Have tried this solution but problem remains there.

One approach is to desterilize the string to some model and then again serialize, but in this case problem is that my model is not defined. User can set any number of fields in the form which may even repeat. User want to get back the controls on same position which he set on form creation. Here is form example.

Form example

In database column type is Text. If I copy value from column and validate in JSON Formatter the value is validated as correct JSON. Here is database value:

[
  {
    "type": "text",
    "label": "Name",
    "className": "form-control",
    "name": "text-1605888860074"
  },
  {
    "type": "radio-group",
    "label": "Gender",
    "inline": true,
    "name": "radio-group-1605892686644",
    "values": [
      {
        "label": "Male",
        "value": "Male",
        "selected": false
      },
      {
        "label": "Female",
        "value": "Female",
        "selected": false
      }
    ]
  },
  {
    "type": "date",
    "label": "D.O.B.",
    "className": "form-control",
    "name": "date-1605892732455"
  },
  {
    "type": "checkbox-group",
    "label": "Disabled",
    "inline": true,
    "name": "checkbox-group-1605892754084",
    "values": [
      {
        "label": "Yes",
        "value": "Yes",
        "selected": true
      },
      {
        "label": "No",
        "value": "No",
        "selected": false
      }
    ]
  }
]

But following is the schema value returned in API response:

"[\r\n  {\r\n    \"type\": \"text\",\r\n    \"label\": \"Name\",\r\n    \"className\": \"form-control\",\r\n    \"name\": \"text-1605888860074\"\r\n  },\r\n  {\r\n    \"type\": \"radio-group\",\r\n    \"label\": \"Gender\",\r\n    \"inline\": true,\r\n    \"name\": \"radio-group-1605892686644\",\r\n    \"values\": [\r\n      {\r\n        \"label\": \"Male\",\r\n        \"value\": \"Male\",\r\n        \"selected\": false\r\n      },\r\n      {\r\n        \"label\": \"Female\",\r\n        \"value\": \"Female\",\r\n        \"selected\": false\r\n      }\r\n    ]\r\n  },\r\n  {\r\n    \"type\": \"date\",\r\n    \"label\": \"D.O.B.\",\r\n    \"className\": \"form-control\",\r\n    \"name\": \"date-1605892732455\"\r\n  },\r\n  {\r\n    \"type\": \"checkbox-group\",\r\n    \"label\": \"Disabled\",\r\n    \"inline\": true,\r\n    \"name\": \"checkbox-group-1605892754084\",\r\n    \"values\": [\r\n      {\r\n        \"label\": \"Yes\",\r\n        \"value\": \"Yes\",\r\n        \"selected\": true\r\n      },\r\n      {\r\n        \"label\": \"No\",\r\n        \"value\": \"No\",\r\n        \"selected\": false\r\n      }\r\n    ]\r\n  }\r\n]"

API Response in Postman

Here is API code snippet of how i'm returning data to client:

var form = _context.Forms.Where(f => f.Id == user.AssignedFormId).FirstOrDefault();

dic.Add("form", new UserAssigendFormModel
            {
                Id = form.Id,
                Title = form.Title,
                Schema = form.Schema
            });
                
_response.error = false;                
_response.data = dic;
_response.message = ResponseMessageKeys.success;

return Ok(_response);

Its really disturbing, any suggestions what is missing?

Ahmad Ahsan
  • 187
  • 3
  • 18

1 Answers1

0

UPDATE have you tried this ?

line.Replace(@"\", "");

Taken from Remove '\' char from string c#

Compare the inpunt string of what FormBuilder generates and the output after you clean up and remove the special symbols

When you copy/paste and shows a formated JSON with linebreaks means that it was stored with those symbols in it.

The approach about the models is not bad, you just need to make it generic for your scenario

enum ControlType {
    Combo,
    Radio,
    Input, //etc
} 
class FormControls {
    ControlType type;
    string label ;
    boolean inline; 
    //etc
}

class CustomForm {
    Guid id ;
    string Description ;
    List<FormItem> lstControls ;
}

CustomForm is a list of FormControls which has all the fields that you need for all kind of formControls, and ControlType is an enumerations with all the type of control.

Then you can parse the JSON and create a CustomForm with all the FormItems that are specified in the JSON

That CustomForm is what you serialize/deserialize into your database

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • using this approach then we'll have another challenge to keep record of controls position. I have updated the original post and briefly mentioned in UPDTAE exactly what i'm looking for. – Ahmad Ahsan May 20 '21 at 06:40