1

I have an array as follows contained in $config:

Array
(
    [1] => Array
        (
            [outlet] => facebook
            [thumbnail] => /images/catalog/modules/videoplayer/Original.jpg
            [url] => http://www.google.com
            [copy] => this is a test
            [vidurl] => http://www.youtube.com
            [poster] => none
        )

    [2] => Array
        (
            [outlet] => facebook
            [thumbnail] => /images/catalog/modules/videoplayer/limon2.jpg
            [url] => http://www.yahoo.com
            [copy] => Here is the copy
            [vidurl] => http://www.vimeo
            [poster] => no poster
        )

)

I'm converting it into JSON as follows:

$module->configuration = json_encode($config,JSON_PRETTY_PRINT);

Everything saves to the DB fine, producing this object upon retrieval:

App\Module Object
(
    [table:protected] => modules
    [timestamps] => 
    [connection:protected] => 
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [attributes:protected] => Array
        (
            [id] => 3
            [title] => wddwdw
            [type] => socialfeed
            [configuration] => {
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

        )

    [original:protected] => Array
        (
            [id] => 3
            [title] => wddwdw
            [type] => socialfeed
            [configuration] => {
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

        )

    [relations:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [casts:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

    [morphClass:protected] => 
    [exists] => 1
)

As you can see the json is broken. When I retrieve $model->configuration it is broken:

{
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

Is there something else I need to do before writing the json to the DB to make sure it is retrievable?

2
  • Which version of Laravel are you using? And how do you decode the JSON after fetching it from the database? Commented Aug 16, 2015 at 22:14
  • 1
    You have exact 256 bytes of text in your "broken json", what is your database structure for the field like? Are there any data truncation warnings? Commented Aug 16, 2015 at 22:16

1 Answer 1

1

Like said @Scuzzy in the comments, i think we have a length problem here. Either you used a varchar (255) to store this JSON in your DB, then that's why it's cut, because your string is too long OR maybe it's just that your var_dump or print didn't show everything and your object is fine.

I think it's probably the first case, and i would suggest to store it as a serialized array instead of JSON in the db.

Sign up to request clarification or add additional context in comments.

6 Comments

If it's the problem with length, then changing the serialization format won't help. But I agree that it looks like field length limit cutting off part of the string.
oh i'm sorry, there's a misunderstanding, i was just telling him to serialize instead of json just beacuase it's better for DB i think, it wasn't related to the main question, just a side advice. :) @jedrzej.kurylo
I see. Out of curiosity: why would it be better? A string is a string.
@jedrzej.kurylo well it's a quite long topic but, serialize is specific to PHP so it doesn't care about encoding, JSON only deals with utf-8 beacause it's meant to be general and exportable. Also when you deal with associative arrays, json_decode will bring them back as objects in stead of arrays, beacuse in js arrays don't have keys so the config here which is an array will be rendered back as a Std object instead of associative array.
You can decode a JSON to an associative array by passing true as a second argument to json_decode. String is a string, so both JSON and serialized array need to properly encode non-ascii characters. Plus, there are popular databases like PostrgreSQL, that support JSON as a field type and allow JSON stored in the database to be queried.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.