Anjali
Anjali

Reputation: 2698

remove \r\n with newline in json string

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

Upvotes: 2

Views: 15230

Answers (3)

Serhat
Serhat

Reputation: 638

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("\\\"", "\"");

Upvotes: 0

rimi
rimi

Reputation: 775

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

Upvotes: 1

Vijayakumar Natarajan
Vijayakumar Natarajan

Reputation: 63

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

enter image description here

Upvotes: 1

Related Questions