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?

NotaGuruAtAll
  • 503
  • 7
  • 19
  • Which version of Laravel are you using? And how do you decode the JSON after fetching it from the database? – jedrzej.kurylo Aug 16 '15 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? – Scuzzy Aug 16 '15 at 22:16

1 Answers1

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.

darkylmnx
  • 1,891
  • 4
  • 22
  • 36
  • 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. – jedrzej.kurylo Aug 16 '15 at 22:48
  • 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 – darkylmnx Aug 16 '15 at 22:53
  • I see. Out of curiosity: why would it be better? A string is a string. – jedrzej.kurylo Aug 16 '15 at 22:59
  • @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. – darkylmnx Aug 17 '15 at 00:34
  • 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. – jedrzej.kurylo Aug 17 '15 at 00:38
  • @jedrzej.kurylo well you need to read this http://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize or http://techblog.procurios.nl/k/news/view/34972/14863/cache-a-large-array-json-serialize-or-var_export.html if we are dealing with only php it's better to serialize the fact that DB like Postgre support JSON is not really relevant to the current situation here because the db is unknown what we know is that we are storing the config in an object so i guess using something which handles objects is better. WP does, Drupal does it etc – darkylmnx Aug 17 '15 at 00:46