Searched for tag JSON and found 3 results in 0.6 ms

PHP: Simplify sending a JSON object to browsers

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

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.

It's 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. Once loaded Mustache.render(template_str, data_obj) gets you a formatted string. 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 test.

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