Send JSON object as string not array

convert string to json
json.parse array of objects
get value from json object in javascript
json string to array
convert array to json javascript
load json file into javascript
json.stringify pretty
json array

I have a string that looks like this

{ "name":"John", "age":30, "city":"New York"}

I want to post it to a PHP file exactly in the format as seen above, not as an array. The reason is that I will later use this string somewhere else, and I have no need to implode it or reformat it.

Here is what I have now.

var sentdata = '{ "name":"John", "age":30, "city":"New York"}';

$.ajax({
    url: 'save.php',
    type: 'post',
    data: sentdata,
    success:function(response){
    console.log('Updated'); 
    }

});

PHP is currently outputting this:

Array ( [{"name":"John","age":30,_"city":"New_York"}] => )

But I wanted a basic string that looks exactly like in sentdata.

I prefer a Jquery/JSON solution, but if I have to do something in PHP then so be it.

Thanks!

The code should look this, notice the change to JS code

data: {data: sentdata}

var sentdata = '{ "name":"John", "age":30, "city":"New York"}';

$.ajax({
    url: 'save.php',
    type: 'post',
    data: {data: sentdata},
    success:function(response){
    console.log('Updated'); 
    }

});

Then you have string variable in PHP code:

$_POST["data"]

JSON.stringify(), a web server. When sending data to a web server, the data has to be a string. Convert a JavaScript object into a string with JSON.stringify() . Stringify a JavaScript Array. It is also In JSON, functions are not allowed as object values. The output of the Parse is an indexed array, but Select is designed to Select based on a Key value. you can access the values in the array directly using JSON notation in a compose statement. If you will send a sample copy of your output used in the Parse I can explain what the JSON notation would look like.

You haven't set the Content-Type of the request, so PHP thinks you are sending application/x-www-form-urlencoded data.

Since your string doesn't have an = in it, it treats this as a key without a value and populates $_POST with that data.

You need to set the Content Type request header explicitly.

$.ajax({
    url: 'save.php',
    contentType: "application/json",
    // etc

However PHP will not automatically process JSON encoded requests. $_POST will be empty.

You need to parse the JSON manually as described in this question.

JSON.stringify(), The JSON.stringify() method converts a JavaScript object or value to a JSON If this parameter is not provided (or is null ), no white space is used. String-keyed array elements are not enumerable and make no sense in� This is what i would go for, just use the original JSON and not a string - As it's using Postman to POST the request payload, JSON.stringify() won't work as the app will fail the validation as it checking that the schema is valid JSON. – Danny Dainton Jan 18 '18 at 9:20

You can still send as an object, but add the string as the value itself.

var sentdata = ['data' : '{"name":"John", "age":30, "city":"New York"}'];

Then just access it as $_POST["data"]

Working with JSON, This is not a big issue — JavaScript provides a global JSON object that has object — strings, numbers, arrays, booleans, and other object literals. We are then passing that object to two function calls — the first one fills the� Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Can't get a JSON object in response to an Ajax request with wp_ajax , log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not� Taking that and running JSON.parse () on it converts it to a non-escaped string, so running JSON.parse () again creates an object. If you do not stringify your string, it will convert to an object and often it is not necessary but if you have no control over the obtained value, then running JSON.parse () twice will do the trick.

JSON Data Types, First four data types (string, number, boolean and null) can be referred as simple data types. Other two data types (object and array) can be referred as complex data types. Double quote; \\ – Backslash; \/ – Forward slash; \b – Backspace; \f – Form Although technically not a value Type, null is a special value in JSON. If you send functions using JSON, the functions will lose their scope, and the receiver would have to use eval() to convert them back into functions. Browser Support The JSON.stringify() function is included in JavaScript (ECMAScript) and is supported in all major browsers.

How to send a JSON object to a server using Javascript , How to send data of HTML form directly to JSON file? JSON | modify an array value of a JSON object � How to convert JSON string to array of� Converting JavaScript Object to String. In this step, we will focus on converting a JavaScript object into a JSON string using JSON.stringify() function. Just the way we converted an array to a string, the same we will transform JavaScript object into string in the given below example.

Comments
  • You need to JSON.stringify() the sentdata, and set the contentType: 'json' on the request
  • You should receive the data just the way you sent it, post your save.php to see why the data was transformed.
  • I can hardly imagine where you can use json object as string though.
  • @Taplar sentdata is already a string and the correct content type is application/json
  • @Musa - all I have is $data = $_POST['data']; echo $data;
  • Uncaught SyntaxError: Malformed arrow function parameter list
  • Not correct js syntax, it should be var sentdata = {data: '...'}