Formatting JSON
Results 1 to 6 of 6

Thread: Formatting JSON

  1. #1
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807

    Formatting JSON

    I came up with this little trick to format JSON output for a little test tool I was making:
    PHP Code:
    echo json_encode(json_decode($json_string), JSON_PRETTY_PRINT); 
    It seems a bit silly: converting to an array so that I can then apply the pretty-print parameter to json_encode(), but then it is likely more efficient than some user-defined function to figure out where to add newlines and tabs/spaces. I was just wondering if I was missing anything in the standard library somewhere that would get the same results from an unformatted JSON string -- or is this about as good as anything?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Flanders Fields
    Posts
    5,833
    I don't have a better solution, but would like to point out that there are no other json_* functions you can call that might be of use.

    You could try to get the JSON formatted at the source? But watch out because newlines in JSON code will break that json code for certain encoding/decoding libraries. I.e., pretty-printed JSON is not valid for certain functions.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    384
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    That decode/encode is probably the best available way to do it -- it just feels kind of stupid to me for some reason. But then it is a display consideration, and PHP is not about client-side display, so . . . \_(ツ)_/
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    384
    PHP Code:
     echo json_encode(json_decode($json_string));
     echo 
    $json_string
    Would be another way,

    build arrays. convert to JSON and send.
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  6. #6
    Senior Member
    Join Date
    Apr 2003
    Location
    Flanders Fields
    Posts
    5,833
    Quote Originally Posted by NogDog View Post
    That decode/encode is probably the best available way to do it -- it just feels kind of stupid to me for some reason. But then it is a display consideration, and PHP is not about client-side display, so . . . \_(ツ)_/
    I would do it without a second thought. PHP built-in functions are probably fast and robust -- although you should probably check to make sure the original JSON is valid instead of just feeding on function result to the other?

    Googling around you'll find plenty of little dinky libraries. I tend not to trust dinky little libraries. Only use them if there's nothing else.
    https://gist.github.com/GloryFish/1045396

    Also, the popular stackoverflow answer likes your solution too:
    http://stackoverflow.com/questions/6...-json-with-php
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •