Transmit JSON data with unexpected field names

I have an object in JavaScript with keys as selectors:

{
    "[data-index="0"]": {
       // something
    },
    "> .class > .one": {
       key: "very bad+ \"\' string"
    }
 }

Is there a way to send this object via AJAX without altering the keys and ensuring the values are correctly formatted?

In terms of correct formatting, I mean that the "very bad+ \"' string" needs to be fully escaped while preserving all characters and symbols for storage in a database.

PS. I am aware of how to send objects via AJAX.

Answer №1

JSON.stringify will convert your object to a JSON string, including special characters.

var json = JSON.stringify(myObject);

Answer №2

When you make slight changes to your object

var obj = {
    "[data-index='0']": {
        // doing something
    },
    "> .class > .one": {
        key: "a very tricky- \"\' string"
    }
}

Then using JSON.stringify(obj) will be successful. The additional pair of " following data-index= causes an issue.

-- modified syntax

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

Extract the TypeScript source code to find the interface associated with a specific React component

Is there a way to access props from a React class declared in an interface associated with that class? Here's an example of the code: interface SomeProps { text: string; label?: string; } class SomeClass extends React.Component<SomeProps& ...

Tips for traversing a nested JSON array successfully

Here is the code snippet I am working with: <div class="modal-body"> <p ng-repeat="maintGroup in maintGroups"> Some text in the modal and {{ maintGroup.serviceGroup.services[$index].description.custDescription }} </p> </div> &l ...

Is it possible to paginate a json response partially?

Curious if a JSON can be partially paginated. Take this example: { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!", "body": "The shortest article. Ever." } }], "included" ...

Sending data from an HTML textarea to a PHP variable without user input

After spending more than two days searching for a solution to this problem, I am now reaching out directly with my question. Although there have been some hints and partial answers, nothing has definitively resolved the issue I am facing, prompting me to m ...

Locate the nearest date (using JavaScript)

Searching for the nearest date from an XML file. <?xml version="1.0" encoding="UTF-8"?> <schedule> <layout fromdt="2014-01-01 00:00:00" todt="2014-01-01 05:30:00"/> <layout fromdt="2014-02-01 00:00:00" todt="2014-01-01 05 ...

"Enhance Your Website with a Custom Contact Form using Bootstrap, jQuery Validation

I am currently working on integrating a straightforward Bootstrap contact form with AJAX and jQuery validation. Despite being close to the desired outcome, there are a few issues that I cannot seem to resolve. The form validates input fields (although I ai ...

As my character slides off the moving platform in this exciting Javascript canvas game, my heart

Can anyone help me figure out how to keep my player on the moving platform? I'm not sure if I need to add gravity or something else. I'm still learning the ropes. export function checkTopCollision({ item1, item2 }) { return ( item1.y + item ...

Validation of Style

Is there a way to translate the following CSS code into JavaScript? Basically, I want it to apply the style rules after the onchange event. .appnitro input:required:valid, .appnitro textarea:required:valid { box-shadow: 0 0 5px #5cd053; border-c ...

how to use ajax to retrieve a value returned by a php function

I have two different files, one named index.php and the other called get_content.php. Strangely, I am unable to display anything on the get_content.php file. I am now left pondering where the issue might be - in index.php or get_content.php? To view the F ...

Scrolling and adjusting window size while perfectly displaying images pixel by pixel using php and javascript

I'm currently working on a forum concept that will feature images. I'm looking to have the image displayed at default width and height, but also want users to be able to resize the window to view more of the image as needed. Additionally, I would ...

What is the best way to ensure that messages stay saved in my app for an extended

I am looking for a way to store messages sent through my small messaging app in a persistent manner. Currently, the messages are transferred from the front-end to a Node, Socket.io, and Express back-end. A friend recommended using Enmaps (), but unfortuna ...

Prevent rapid flashing by adding a delay to the spinner in the jQuery Validations plugin

I'm currently utilizing the jQuery validation plugin available at http://docs.jquery.com/Plugins/validation for implementing client-side validations. For a remote validation to verify the availability of a username in the database, I intend to prolon ...

Utilizing an Entity's Location for triggering an Action in AFrame

Looking to create a custom component that can track the position of a sphere within an AFrame scene and trigger an event when it reaches a specific coordinate (for example, resetting to its default position as shown below): AFRAME.registerComponent("t ...

Can you explain the significance of this regular expression?

I'm trying to decipher the meaning of this regular expression. Can anyone help? "^[A-Z]{3}-[4-7]\d{2,4}\$$" My understanding is that it must start with exactly 3 uppercase letters and end with a sequence of 2, 3, or 4 digits (although I a ...

React: the value of this.props.item has not been defined

When attempting to pass an array from one component to another and then to a third component, the item is showing up as undefined. In my App.js file, I am trying to pass this.state.searchResults to the SearchResults component. import React from 'rea ...

Developing JSON without any indentation for arrays

When dealing with large JSON files containing numerous lists and elements, I often find myself wanting to achieve a specific structure on the left side instead of the default right structure provided by Newtonsoft library. Are there any other libraries tha ...

Only selecting a handful of items from a JSON structure

I'm currently working with Laravel 5.5.* and jQuery (jquery-3.3.1.min.js). While I primarily develop in PHP, delving into jQuery poses a unique challenge for me, thus prompting the need for assistance. My current project involves creating a landing ...

javascript code to combine two JSON objects with numeric string keys and sort them in ascending order

Is there a way to combine two JSON objects with numerical string keys and arrange them in ascending order? let obj1 = { '10' : "ten" '2' : "two", "30": "thirty } let obj2 = { '4' : "four", '5' : "five", "1": "on ...

How to make YouTube links open in the app on iOS instead of Safari

Is there a way to automatically open a YouTube link in the YouTube app instead of Safari on mobile? I haven't been able to find any information on this. Any guidance would be greatly appreciated! Thank you for helping out, your assistance means a lo ...

Searching for getStaticPaths using a GROQ query that involves nested dynamic routing

My NextJS project has a complex nested folder structure. You can view the layout here. Utilizing Sanity as my CMS, the getStaticPaths function within my index.js file is functioning properly: export const getStaticPaths = async () => { const routes ...