Support

Account

Home Forums General Issues Can I save json/ serialized array to a text field?

Helping

Can I save json/ serialized array to a text field?

  • I have a php array that I’m converting to json with json_encode() and saving with update_field().

    I can retrieve the json with get_field() and echo it out; if I paste it in to jsonlint, it’s valid and everything seems fine.
    However, I need to convert the json back to a php array when I get it back but, when I use json_decode() and echo it back out, I get an error that says “quoted object property name expected” (all the property names are quoted and the json is valid – when I copy and paste, at least)

    If I copy and paste the output get_field() and run it through json decode, it works. So the json is valid. It’s not the json. for some reason, if I try to json_decode the get_field output directly, I get null and it throws the error.

    I tried using serialize() instead but that won’t save at all so no luck there.

    I just need to save an associative array to a field here; json seems like the best bet but I’m open to other suggestions. Does anyone know what’s happening to my json from the get_field() output that’s causing it to be malformed?

  • You need to set the second parameter of json_decode to true http://php.net/manual/en/function.json-decode.php

    
    $array = json_decode($json, true);
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.