code
code

Reputation: 1081

Remove double quotes from json on specific key value

I have a json file that is structured like below. I need to remove the double quotes from the source "value"

{
    tags:[
        {
            name: "video",
            cssanimate: "flipInY",
            source: "{ mp4: '1.mp4' }"
        }
    ]
}

I need it to be:

{
    tags:[
        {
            name: "video",
            cssanimate: "flipInY",
            source: { mp4: '1.mp4' }
        }
    ]
}

I thought maybe I could do something with preg_replace but only for the "source key". Is something like this possible?

$json = json_encode($records, JSON_PRETTY_PRINT);
$json = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $json);

Any help would be appreciated :)

Upvotes: 0

Views: 461

Answers (1)

aynber
aynber

Reputation: 23001

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'])
                  )
        );
}

Upvotes: 1

Related Questions