Searched for tag JSON and found 5 results in 0.7 ms

PHP: Simplify sending JSON to a browser

I can never remember the correct MIME type for JSON so I wrote this wrapper function to simplify sending JSON to a browser.

// Convert a PHP data structure to JSON and send it to the browser
function json_output($obj) {
    $output = json_encode($obj, JSON_INVALID_UTF8_SUBSTITUTE);

    // http://tools.ietf.org/html/rfc4627
    header('Content-type: application/json');
    print $output;
    exit;
}
Tags:
Leave A Reply

YAML is growing on me

The more I learn about YAML, the more I like it. JSON is great as a machine readable format, but it sucks at being human readable. Want to add an element, better make sure you have all the commas and curly braces in the exact right place or the whole thing will be unusable. YAML on the other hand is designed to be human readable and modifiable. It's a very simple key/value system using indentation to represent layers.

PHP has a PECL module with good YAML support. There are also pure PHP versions if you're unable to install PECL modules. Symfony provides one, and so does Spyc. I prefer the latter because it's a single file and very easy to install.

On the Perl side there is YAML::XS, YAML::PP, and many others.

Parsing YAML is very easy in just about every language I can find. If you have a complex data structure that you need humans to interact with use YAML please.

Here is a great breakdown of when to use YAML vs JSON:

Use Case Recommended Why
API request/response JSON Universal support, strict parsing
Configuration files YAML Comments, readability
Browser/JavaScript JSON Native parsing
Kubernetes/Docker YAML Industry standard
Data interchange JSON Unambiguous, fast
Human-edited files YAML Less punctuation

YAML spec differences.

Tags:
Leave A Reply

Comparison of markup languages

I've been investigating JSON vs YAML vs TOML for various applications. After testing many variations I ended up writing a simple tool to compare all three live.

Tags:
Leave A Reply

Mustache is a very simple and powerful templating language

I am late to the game learning about the Mustache templating language. It is very mature, and has support for all of the languages I care about: Perl, PHP, and Javascript. I am mainly focused on using it in Javascript as it is a great way to format raw data you get via JSON into something more human consumable like HTML.

Mustache.js is incredibly easy to implement and get started with. A single include loads the entire zero-dependency library. As of v4.2.0 the library is tiny, clocking in at only 25k. A big selling point is the ease of installation and use. You can be up and running with Mustache.js in less than 5 minutes.

<script src="/js/mustache.min.js"></script>
var data = { name: "Jason", animal: "Kitten" };
var str  = Mustache.render("<p>Hello {{name}}, I hear you like {{animal}}s</p>", data);

I liked it so much I wrote up a Mustache sandbox to allow me to play around with the templating system live. The template syntax is a little archaic, but it is workable.

Note: They even have Arduino support, which I need to investigate.

Tags:
Leave A Reply

Tasmota API via HTTP using Curl

Tasmota has a huge catalog of commands that can be run via the serial console, MQTT, or HTTP. These commands are great for scripting purposes. The easiest way I've found to send a command is with Curl.

curl http://192.168.1.142/cm?cmnd=POWER+TOGGLE

The API will answer in JSON which is easily digestible with JQ.

curl -s http://192.168.1.142/cm?cmnd=STATUS+8 | jq
Tags:
Leave A Reply - 3 Replies