bumbumpaw
bumbumpaw

Reputation: 2528

Extra slashes on json response

On database: { "email", "color" }

But when I try to output it using application/json' => \yii\web\Response::FORMAT_JSON, the string contains extra slashes

[
      "Verify",
      "{ \"email\", \"color\" }"
    ]

I know that I can sort of use the replace() but can someone enlighten me on this scenario?

Upvotes: 1

Views: 2399

Answers (3)

meda
meda

Reputation: 45490

It seems to me that you are trying to encode an already json encoded data.

As a results it escapes the characters.

You have 2 options:

  1. Do not format it to JSON again
  2. Decode the data with json_decode() before return

If you need to append or combine that json data, then #2 is your only option

Upvotes: 1

Steve Harris
Steve Harris

Reputation: 344

@webDav almost has the correct answer.

It appears you are incorporating the data from the database into another data structure. So you will need to parse the JSON from the database first, then include that data in your data before encoding again.

You are not retrieving raw string (similar to JSON) from the DB, but it is a string that needs to be converted to JSON in order to have meaning.

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133370

the encode add the slashes for prevent improper quotes sequence that break the corret use of content

whitout slashes you have in yoru case

  "{ "email", "color" }"  // that is not correctly formateed 

you have already quote around you value in database.. and you data are already in json format so so you could use without encode

Upvotes: 1

Related Questions