2

I created a JSON string in C# using this code.

string jsonFormatted = JValue.Parse(JSONresult).ToString(Formatting.Indented);

when I paste the json Formatted string in notepad++, I get lot of \r\n and "\". I want to replace all \r\n with newline. when I tried to replace \r\n with empty space, all \r\n goes away and I can format the string using JSON Viewer -Format JSON plugin, but all \r\n are replaced by LF. Below is the screen shot:

enter image description here

I want the \r\n to be replaced by new line CRLF. My JSON file is huge so it is difficult to change all \r\n by hand.

enter image description here

Below is the sample of my partial JSON string:

"{\r\n  \"header\": {\r\n    \"tenantId\": \"23213\",\r\n    \"requestType\": \"PreciseIdOnly\",\r\n    \"clientReferenceId\": \"3243214\",\r\n    \"expRequestId\": \"\",\r\n    \"txnId\": \"\",\r\n    \"messageTime\": \"2020-06-05T19:35:45Z\",\r\n 

Can I do this in either C# or notepad++ or any other editor.

replacing \r\n with empty space or string.empty is not working because the newline character does not come up in notepad++ if I replace the string with \r\n. I want a new line too along with \r\n gone.

Below is my entire JSON file

{
    "header": {
        "tenantId": "23213",
        "requestType": "PreciseIdOnly",
        "clientReferenceId": "3243214",
        "expRequestId": "",
        "txnId": "",
        "messageTime": "2020-06-05T19:35:45Z",
        "options": {}
    },
    "payload": {
        "control": [
            {
                "option": "SUBSCRIBER_PREAMBLE",
                "value": "23213"
            },
            {
                "option": "SUBSCRIBER_OPERATOR_INITIAL",
                "value": "qq"
            },
            {
                "option": "SUBSCRIBER_SUB_CODE",
                "value": "1231"
            },
            {
                "option": "PID_USERNAME",
                "value": "abc"
            },
            {
                "option": "PID_PASSWORD",
                "value": "aaa"
            },
            {
                "option": "PRODUCT_OPTION",
                "value": "24"
            }
        ],
        "contacts": [
            {
                "id": "APPLICANT_CONTACT_ID_1",
                "person": {
                    "typeOfPerson": "",
                    "personIdentifier": "",
                    "personDetails": {
                        "dateOfBirth": "2020-06-05",
                        "yearOfBirth": "",
                        "age": "",
                        "gender": "",
                        "noOfDependents": "",
                        "occupancyStatus": "",
                        "mothersMaidenName": "",
                        "spouseName": ""
                    },
                    "names": [
                        {
                            "id": "",
                            "firstName": "test1",
                            "middleNames": "test2",
                            "surName": "test3",
                            "nameSuffix": ""
                        }
                    ]
                },
                "addresses": [
                    {
                        "id": "Main_Contact_Address_0",
                        "addressType": "CURRENT",
                        "poBoxNumber": "",
                        "street": "42123 test drive",
                        "street2": "",
                        "postTown": "a",
                        "postal": "33232",
                        "stateProvinceCode": "qa"
                    }
                ],
                "telephones": [
                    {
                        "id": "Main_Phone_0",
                        "number": ""
                    }
                ],
                "emails": [
                    {
                        "id": "MAIN_EMAIL_0",
                        "type": "",
                        "email": ""
                    }
                ],
                "identityDocuments": [
                    {
                        "documentNumber": "12321343",
                        "hashedDocumentNumber": "",
                        "documentType": "SSN"
                    }
                ]
            }
        ],
        "application": {
            "productDetails": "",
            "applicants": [
                {
                    "contactId": "APPLICANT_CONTACT_ID_1",
                    "applicantType": "APPLICANT"
                }
            ]
        }
    }
}

Any Help will be highly appreciated

Anjali
  • 2,540
  • 7
  • 37
  • 77
  • Does this answer your question? [How can I remove "\r\n" from a string in C#? Can I use a regular expression?](https://stackoverflow.com/questions/1981947/how-can-i-remove-r-n-from-a-string-in-c-can-i-use-a-regular-expression) – hendrixchord Jun 06 '20 at 04:34
  • No, I tried to replace \r\n with string.empty. When I do that, the new line character is gone. I want newline too. – Anjali Jun 06 '20 at 04:38
  • 2
    `\r\n` **is** a newline. Why do you want to remove it? Your question seems to be "how do I replace a newline with a newline?" which is, shall we say, an odd question to ask. – mjwills Jun 06 '20 at 08:08
  • If I well understand, you want to replace `\r\n` with `\n`, am I right? – Toto Jun 06 '20 at 08:19

3 Answers3

1

Try Disabling the 'Show All Characters' option in notepad++ to see how your json is getting formatted.

enter image description here

1

Same thing happened to me too. What you can do is replace all \r\n with space in notepad++ something like this:

enter image description here

and then replace all \n with \r\n. Make sure Extended \n\r\t i selected in replace window and you will get all CRLF in your notepad. Let me know if you see any issues

rimi
  • 624
  • 5
  • 15
0

Here is how you can replace \r\n with \n in C#

string responseString = "your json data which includes \r\n";

// replace \r\n with \n
string data = responseString.Replace("\r\n", "\n");
// replace \" with "
string refinedString = data.Replace("\\\"", "\"");
Serhat
  • 216
  • 2
  • 17