Remove Double-Quotes from a JSON_Encoded String on the Keys

Remove double-quotes from a json_encoded string on the keys

EDITED as per anubhava's comment

$str = '{"start_date":"2011-01-01 09:00","end_date":"2011-01-01 10:00","text":"test"}';
$str = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $str);
echo $str;

This certainly works for the above string, although there maybe some edge cases that I haven't thought of for which this will not work. Whether this will suit your purposes depends on how static the format of the string and the elements/values it contains will be.

json_encode remove quotes from keys?

When I test this portion of code :

echo json_encode(array( 'foo' => 'bar'));
die;

I get :

{"foo":"bar"}

Which is valid JSON.

(Note these are double-quotes, and not simple-quotes as you posted)


The ouput you are asking for :

{ foo : 'bar' }

is valid Javascript, but is not valid JSON -- so json_encode will not return that.

See json.org for the specification of the JSON format -- which is a subset of Javascript, and not Javascript itself.


Instead of "stripping the quotes out myself with some ugly regex", you should adapt your code, so it accepts valid JSON : this is way better, in my opinion.

Remove double quotes from json on specific key value

There is an already-encoded json in the source field of your database, so you just need to manipulate it before you put it in the array:

$records3 = array();
while ($row3 = $result3->fetch_assoc()) {
$records3[] =
array('tags' =>
array(
'name' => $row3['name'],
'cssanimate' => $row3['cssanimate'],
'source' => json_decode($row3['source'])
)
);
}

Remove double quote in json_encode()

If you add an underscore to your regex at the end it will do it.

$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$array_final);

I assume that's what that preg_replace is for anyway.

Remove double quotes from json_encode array

If Google wants it in that particular format, simply feed him :)

I supposed the json_encoded result stored in $json.

So, you can do this:

$json = str_replace('"','', (string) $json);

remove quotes from keys in a json string using jquery

Use Regular Expressions:

var a='{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head","phone" : "9789234793","email" : "surfingkumar@gmail.com","role" : "Admin",    "empId" : "EI003","reportingto" : "KumarP"}]}';
a=a.replace(/"(\w+)"\s*:/g, '$1:');
alert(a);

The string will become as your second codeblock:

{Table: [{userid: "11",name: "KumarP",designation: "Business Head",phone: "9789234793",email: "surfingkumar@gmail.com",role: "Admin",    empId: "EI003",reportingto: "KumarP"}]}

But won't that cause a problem if the label was a reserved word?



Related Topics



Leave a reply



Submit