Securing JSON from XML special characters

I have a JSON object that may include invalid XML characters, for example:

{
   items: [
      { id: 1, description: 'hello&nbsp;<b>world</b>&shy;' }
   ]
}

These values are inputted into a textarea by the user, typically through copying and pasting. Due to legacy reasons, I bind properties like description to XML and then later parse the XML on the server.

It is crucial to ensure that the XML generated from the JSON data is valid, which means we need to escape the values in the JSON. If left unescaped, it would result in invalid XML like this:

<data>
   <myItems>
       <item id="1">hello&nbsp;<b>world</b>&shy;</item>
   </myItems>
</data>

What is the best way to escape the JSON content to ensure it produces only valid XML?

Requirement: The function escape(json) should produce the same output as escape(escape(json))

Answer №1

Is there a way to convert json into valid xml format?

There is no need to escape the json to have only valid xml. The conversion process takes care of creating the appropriate XML structure.

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

Fill the dropdown menu with JSON keys

My challenge involves working with an array containing objects, which are sent to the client via a node.js server integrated with mongodb. I need to extract all keys/fields from the object (such as name, surname, telephone) without their values (for exampl ...

Utilizing reactjs (MERN stack) to dynamically update content on a single page based on both URL parameters and database queries

Hello everyone, please excuse my English Imagine I have page1 with content in a database, and page2 with different content in another database. Both page1 and page2 share the same template, but I want to dynamically change the content based on the URL or ...

The JSON.parse function fails to generate correctly formatted arrays of objects

Here is the string I am trying to pass: {"1":{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},"2":{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},"8":{"244":{"cityid":8,"bpid":244 ...

show information retrieved from database according to the drop-down menu selection

Can anyone assist me with this query? I have a drop-down menu that includes "Shop A" and "Shop B". Shop A has a value of 1, and Shop B has a value of 2. When I select Shop A from the dropdown, I want to display the data from the database as a table specifi ...

Tips for sending a JSON response from Node.js to a JavaScript file?

Currently, I am working on plotting data from MongoDB in a graph for display on a website. My approach so far involves fetching the response from MongoDB using Node.js and Mongoose, which then sends a JSON object back to the browser. app.get('/priced ...

What is the best way to accurately determine the height and offset values of an element while utilizing ng-repeat?

My objective is to determine the offset and height of list items. Once I have those values, I trigger a function in a parent directive. Ultimately, this will involve transitioning elements in and out as they come into view. Issue: Due to the use of ng-rep ...

Verify if there are multiple elements sharing the same class and initiate a certain action

I am working with three products that have a similar structure: tickbox | label (same text but different value) | Qty dropdown (different input name) These products fall into three different label "classes": 1st - <label for="related-checkbox-708745 ...

Angular directive within a directive

I'm a beginner in AngularJS and I'm attempting to create a directive that contains another directive inside it. Here is how the first directive looks: ( function () { app.directive("cmEventBar", function () { var controller = func ...

The useNavigate() function seems to be failing to redirect to the '/protected' route

The issue lies in the redirection process after receiving the token from the API. Although the token is successfully saved in local memory, the redirect to the intended page does not occur as expected. Instead, a manual window refresh is required to naviga ...

How to create a fresh factory instance in Angular Js

I have implemented a factory in my application to retrieve a list of folders and display it on the front end. Additionally, I have a form on the front end where users can add new folders to the existing list. After adding a folder, I need to refresh my fac ...

Learn how to generate a dynamic pie chart in PHP that can adjust its sections based on the user's input, giving you a fully customizable data visualization tool

let userData = [ { label: "History", data: 90 }, { label: "Science", data: 85 }, { label: "Art", data: 95 }, ]; I have written this javascript code to represent the user's data, but I want it to be more flexible an ...

Using Swig template to evaluate a condition

I'm trying to achieve something similar using swig-template. var remId = $(this).attr('remId'); if (remId) { var end = remId.search('_'); var underscore = remId.slice(end, end + 1); var Id = remId.slice(end + 1, remId ...

Utilizing jQuery to apply a single function to multiple elements sharing a common class but unique IDs, resulting in varying outcomes

I am currently developing a music website that wants to use album covers as links to the track listings for each release. One challenge I am facing is the inability to add descriptive text alongside the image if it's used as a link, unless using over ...

The AJAX request is failing to reach the server

I'm currently using AJAX to populate a dropdown, but for some reason the call isn't reaching the server. Upon checking Firebug, I see the following error: POST 0 status 404 not found This is the code I'm working with: function selec ...

Unexpected behavior encountered when deserializing nullable boolean with ServiceStack.Text

Let's say I have a class that includes a member with the bool? type. public class Request { public bool? IsOk {get; set;} } I am utilizing ServiceStack's JSON deserializer and expecting that any values other than true or false in a JSON str ...

Launching the node application using `node` as the starting command is successful, however, using `/usr/bin/node` as the starting

My goal is to configure a node application as a service. To start the service, I must initiate node with an absolute path, specifically using usr/bin/node. However, my application seems to malfunction when launched with this absolute path for unknown rea ...

Steps to retrieve the content of a div element and store it in a variable in Ionic 4

I am currently using Ionic 4 and need to extract the text from within my div. Below is the code snippet from home.page.html: <ion-item> <div id="Target" class="editable" [innerHtml]="resume" contenteditable> <p>Lorem ipsum d ...

Instructions on converting XML data obtained from an AJAX call into a downloadable file

I've been struggling with converting an AJAX response containing XML text into a downloadable file. I've tried various methods but haven't had success. In the past, I was able to work with a similar scenario involving a pdf, where the conte ...

Implementing a long-press feature to increment a counter in React Native

Next to my TextInput, there is a button that increments its counter each time it's pressed. <TouchableWithoutFeedback onPress={() => {this.increaseAmount(step, rowID)}}> <View style={styles.amountPlusButtonContainer}> ...

Is it possible to add a class to the parent element when the input value of a sub-child element is

Is there a way to select and add a class to an li element in which the sub-child input is empty when clicking on button #btn-modal-clear? If so, what is the best approach to achieve this? Example HTML Code: <li>...<li> <li> <a c ...