skip to Main Content

I’m receiving some data from API

{
    "id": 1,
    "name": "Merc",
    "content": "{ntt"1": {nttt"topicId": 1,nttt"topicName": "Project Overview",nttt"topicIcon": "icon1",nttt"topicContent": "<h1 class=\"ql-align-center\">Heading</h1><h2 class=\"ql-align-center\">Content1SubHeading</h2><p class=\"ql-align-center\"><strong style=\"color: rgb(230, 0, 0);\"><em><u>Subject</u></em></strong></p><p class=\"ql-align-center\"><strong style=\"background-color: rgb(255, 255, 0);\"><em><u>Content</u></em></strong></p><p class=\"ql-align-center\"><span class=\"ql-font-serif\">Font Check</span></p><ol><li><span class=\"ql-font-serif\">One</span></li><li><span class=\"ql-font-serif\">two</span></li></ol><ul><li><span class=\"ql-font-serif\">Bullet1</span></li><li><span class=\"ql-font-serif\">Bullet2</span></li></ul><p>",
       "hierarchy": "[rn    {rn      "topicId": 1,rn      "topicName": "Project Overview",rn      "topicIcon": "T1 icon",rn      "children": [rn        {rn          "topicId": 4,rn          "topicName": "Introduction",rn          "topicIcon": "T4 icon"rn        },rn        {rn          "topicId": 5,rn          "topicName": "Additional",rn          "topicIcon": "T5 icon",rn          "children": [rn            {rn              "topicId": 6,rn              "topicName": "Requests",rn              "topicIcon": "T6 icon"rn            },rn            {rn              "topicId": 7,rn              "topicName": "Performing",rn              "topicIcon": "T7 icon",rn              "children": [rn                {rn                  "topicId": 8,rn                  "topicName": "Landing Page",rn                  "topicIcon": "T8 icon"rn                }rn              ]rn            }rn          ]rn        }rn      ]rn    },rn    {rn      "topicId": 2,rn      "topicName": "Questionnaire",rn      "topicIcon": "T2 icon",rn      "children": [rn        {rn          "topicId": 9,rn          "topicName": "Reports",rn          "topicIcon": "T9 icon"rn        },rn        {rn          "topicId": 10,rn          "topicName": "Workflow Details",rn          "topicIcon": "T10 icon"rn        }rn      ]rn    }rn  ]"

This response looks wierd, I need to get rid of this rn from content and ntt many other slashes

I Tried using replace(/\n/g, '') & JSON.stringify(JSON.parse(<json object>)) but it didn’t work

2

Answers


  1. TLDR: You don’t need to remove n and r characters. They are used to represent json content inside a property of type string.

    The json type of "hierarchy" (and "content") property is string. And since the specific property’s value is a multiline json definition, it is normal to see the newlines and tabs of the original json represented as escape sequences (n, t, r).

    Now if you try to paste the above string, as is, in some javascript code, keep in mind that in order to provide the actual string value( e.g. inside "hierarchy" property), you will need to either read the full json from a file or enclose it in "String.raw". Otherwise you will need to further escape it with extra chars. This is because n, t and r have already a special meaning in javascript. (For more info on json newlines and to avoid confusion see this post).

    So try pasting the code below (it uses String.raw which makes n,t,r to no have a special meaning javascript-wise) in the browser’s dev console (F12) and you will see that the json contained inside hierarchy property has been parsed correctly to an array of objects.

    
    
    JSON.parse(
        JSON.parse(
          String.raw`{
    "id": 1,
    "name": "Merc",
    "content":"{ntt"1": {nttt"topicId": 1,nttt"topicName": "Project Overview",nttt"topicIcon": "icon1",nttt"topicContent": "<h1 class=\"ql-align-center\">Heading</h1><h2 class=\"ql-align-center\">Content1SubHeading</h2><p class=\"ql-align-center\"><strong style=\"color: rgb(230, 0, 0);\"><em><u>Subject</u></em></strong></p><p class=\"ql-align-center\"><strong style=\"background-color: rgb(255, 255, 0);\"><em><u>Content</u></em></strong></p><p class=\"ql-align-center\"><span class=\"ql-font-serif\">Font Check</span></p><ol><li><span class=\"ql-font-serif\">One</span></li><li><span class=\"ql-font-serif\">two</span></li></ol><ul><li><span class=\"ql-font-serif\">Bullet1</span></li><li><span class=\"ql-font-serif\">Bullet2</span></li></ul><p>"}}",
    "hierarchy": "[rn    {rn      "topicId": 1,rn      "topicName": "Project Overview",rn      "topicIcon": "T1 icon",rn      "children": [rn        {rn          "topicId": 4,rn          "topicName": "Introduction",rn          "topicIcon": "T4 icon"rn        },rn        {rn          "topicId": 5,rn          "topicName": "Additional",rn          "topicIcon": "T5 icon",rn          "children": [rn            {rn              "topicId": 6,rn              "topicName": "Requests",rn              "topicIcon": "T6 icon"rn            },rn            {rn              "topicId": 7,rn              "topicName": "Performing",rn              "topicIcon": "T7 icon",rn              "children": [rn                {rn                  "topicId": 8,rn                  "topicName": "Landing Page",rn                  "topicIcon": "T8 icon"rn                }rn              ]rn            }rn          ]rn        }rn      ]rn    },rn    {rn      "topicId": 2,rn      "topicName": "Questionnaire",rn      "topicIcon": "T2 icon",rn      "children": [rn        {rn          "topicId": 9,rn          "topicName": "Reports",rn          "topicIcon": "T9 icon"rn        },rn        {rn          "topicId": 10,rn          "topicName": "Workflow Details",rn          "topicIcon": "T10 icon"rn        }rn      ]rn    }rn  ]"}`
    ).hierarchy);
    

    Note: I guess that you have not pasted the entire json because there are some unclosed brackets inside the content property. I have closed them by replacing the last " in content property value with with "}}".

    Note2: String.raw will not be needed if you parse directly from the received response. I use it here to provide an easy copy/paste demonstration of parsing a nested json, stuffed inside a string property.

    Note3: It is worth mentioning that the content property was set with linux newlines (n) and hierarchy property was set with windows newlines (rn) and no tabs. The answer to this could be that the source for each of the properties were two different files. e.g. one created in windows and one created in linux.

    Login or Signup to reply.
  2. Your regular expression is wrong, I could remove them by using:

    YourJsonStringVar.replace(/[nr]/g, '')
    

    Usage:

    const jsonString = '{"name": "abc", "id": 30, "content": "123 abc Stnxyz, zzzr10001"}';
    
    const cleanedString = jsonString.replace(/[nr]/g, '');
    
    console.log(cleanedString);
    

    Explanation:

    • [ ]: The square brackets indicate a character class. Any character within the brackets will be matched.

    • n and r: The escape sequences ‘n’ and ‘r’ represent the newline and carriage return characters, respectively.

    • g modifier: The ‘g’ modifier is used to perform a global search, meaning it will match all occurrences of the pattern in a string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search