HTML Content Embedded Within JSON Response

I am struggling with an API that returns a JSON object containing badly formatted values with \r and \n characters. Despite my attempts to use JSON.stringify to convert it into a string and then remove these characters, I still encounter errors when trying to parse the data again.

Response

            {
            "seller_store_name": "test",
            "seller_store_description": "<blockquote>
<p>ewffwewef</p>
<p>wefwef</p>
<p>&nbsp;</p>
<table>
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>fewfewf</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>efwfewf</td>
</tr>
</tbody>
</table>
</blockquote>",
            "seller_profile_picture":"",
            "seller_profile_picture_default": "https://test.com/",
            "seller_banner": "",
            "seller_banner_default": "https://test.com/"
        }

Attempt 1, turning it into a string and removing unwanted characters

sellerData = JSON.stringify(e);

"            {\n            \"seller_store_name\": \"test\",\n            \"seller_store_description\": \"<blockquote>\r\n<p>ewffwewef</p>\r\n<p>wefwef</p>\r\n<p>&nbsp;</p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td>&nbsp;</td>\r\n<td>&nbsp;</td>\r\n<td>fewfewf</td>\r\n</tr>\r\n<tr>\r\n<td>&nbsp;</td>\r\n<td>&nbsp;</td>\r\n<td>efwfewf</td>\r\n</tr>\r\n</tbody>\r\n<table>\r\n</blockquote>\",\n            \"seller_profile_picture\":\"\",\n            \"seller_profile_picture_default\": \"https://test.com\",\n            \"seller_banner\": \"\",\n            \"seller_banner_default\": \"https://test.com\"\n        }\n    "

sellerData = sellerData.replace(/\\r/g,"").replace(/\\n/g,"");
sellerData = JSON.parse(sellerData);

Although this successfully removes the characters, I am unable to turn the data back into an object as it remains a string.

Attempt 2, attempting basic parsing

sellerData = JSON.parse(String(e));

Unexpected token in JSON at position 126

Any assistance would be greatly appreciated. Unfortunately, I do not have the authority to alter the API response.

Answer №1

Give this a shot

tryRemoving = tryRemoving.replace(/\\r\\n/g, "");

My hypothesis is that the issue lies with the presence of \r\n, therefore removing it should resolve the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I transfer an instance of a class to dataTransfer.setData?

So I created a new instance of a class: let item = new Item(); Next, I attempted to serialize the item and add it to dataTransfer for drag and drop functionality: ev.dataTransfer.setData("info", JSON.stringify(item)); At some point, I need to retriev ...

AngularJS is throwing an error claiming that the controller is not defined and is not a function

Struggling to create a basic angular application, every time I attempt it, I encounter this issue and can never find a solution. The content of App.js is as follows: angular.module('Euclid', ['ui.bootstrap', 'ngRo ...

I'm curious about using NextJS to fetch an API with a specific router ID. Can anyone provide guidance on how to achieve this and then render the data as HTML?

Greetings! I am currently coding in NextJS and working on a user page that fetches user-specific information from an API. My goal is to extract the ID query from the URL and use it to make an API request. The API endpoint follows this structure: /Users/{i ...

Retrieving values from nested arrays in Vue.js

I'm currently delving into Vue3. My goal is to extract the values from an array within an array in order to create a neat table. Once extracted, I plan to separate these values with commas. For more information, you can visit this link: https://stack ...

Delivering script files directly from the karma server

I am currently conducting tests on phantomjs using the karma runner, and I have encountered a problem where my ajax calls are consistently failing with a 404 error. After some struggle in determining the correct placement of the file (refer to: Including ...

a single function spread across two controllers[JavaScript]

My query pertains to a JavaScript program with 2 controllers. The issue I am facing is the need for one function in both controllers (data.duration). This function serves two purposes, once for features themselves and once for the summary. Currently, this ...

Implementing CSS styles using a JavaScript variable

I'm facing a unique challenge right now. My JavaScript variable contains CSS rules like this: "floating_icons": { "main_color": "", "phone_color": "", ...

Error: [Errno 22] occurred while attempting to read a JSON file with .read() method

I'm currently facing an issue while attempting to read a JSON file in Python. The file 'Books_5.json' is located in the Downloads folder, where I am operating from. However, when I try to utilize the .read() function, I encounter the followi ...

Is it possible to toggle all parent targets in Bootstrap?

When trying to showcase my point, I believe it is best demonstrated by visiting Bootstrap documentation at https://getbootstrap.com/docs/4.0/components/collapse/ and viewing the "multiple targets section." In this section, you will find three buttons: togg ...

Utilizing Vanilla JavaScript to retrieve information from a HTML table

I am currently running my website on XAMPP and I have a database connection set up that I can use. In my database, I have three drop-down menus labeled Translations, Books, and Chapters where the data for each is stored. My goal is to utilize Vanilla JS ...

What is the best way to extract text directly from a span element without including the text of its child nodes?

I am dealing with a piece of HTML that is dynamically generated, and it looks like this: <span> 10/10/2012 <div class="calendar"> lots of text elements here </div> </span> Is there a way to specifically retrieve the tex ...

What is the best method for incorporating a JSON framework into a project

I've recently obtained the JSON framework DMG file from http://code.google.com/p/json-framework/downloads/list. I then proceeded to follow Option 3 outlined here: http://code.google.com/p/json-framework/wiki/InstallationInstructions. Initially, I set ...

Displaying an image directly in front of the camera using Three.js

I am currently working on a flight simulator project and faced a challenge in placing a cockpit image in front of my camera controlled by FirstPersonControls. Despite trying various methods, I have not been successful in making it work. Here is what I hav ...

Convert the Java.util.Map into a JSON schema declaration

Struggling to find a simple solution, I'm looking to convert a java.util.Map instance into a JSON schema that can be recognized by the org.jsonschema2pojo Maven plugin in order to generate a POJO. I’ve hit a roadblock with this task and could reall ...

Is it possible to store an object within a Liferay entity?

I have defined a Liferay entity using service.xml. Is it possible to store a JSON object within a string column and retrieve this value as an object from the API? Currently, the response for "json" looks like this: { "name": "test", ...

Discover the complete guide on incorporating a JavaScript library with additional dependencies in Angular 2

I am a beginner with angular 2 and I am attempting to integrate the Miso Dataset JavaScript library into my angular 2 project. The Miso library requires other JavaScript libraries as dependencies. Although I have included all the necessary JavaScript file ...

The Dilemma of Mustache Integration with Jquery Plugin

I've implemented mustache js with an accordion plugin, using an external template file and an external json file to populate the accordion widget. However, I'm encountering an issue where the accordion does not seem to work properly unless prompt ...

Can data from an Angular app be accessed by an external JavaScript code within the same project?

I've been thinking about a theoretical scenario that luckily I haven't encountered yet. Imagine I have an Angular Project compiled in My PROJECT FOLDER. <br/> Inside this PROJECT FOLDER, there's another JAVASCRIPT FILE external to ...

Determine if the element in a string is present in an array using TypeScript

I am dealing with the string: "0,2,4,5,6" How can I verify if these numbers exist in an array? let daysweek = [ { id: '0', name: 'Sunday' }, { id: '1', name: 'Monday' }, { id: '2', n ...

Clicking through the button inside Vuetify's text-field append slot

While working on creating Vuetify's v-text-field with a slot named append containing a button, everything seems to be functioning correctly except for the fact that when I click the button, it goes through and focuses on the text field. On mobile devi ...