Q:

How to pretty print JSON into a line based key structure?

Say I have an arbitrary nested JSON file:
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
     {"value": "Open", "onclick": "OpenDoc()"},
     {"value": "Close", "onclick": "CloseDoc()"}
   ]
 }
}}

And I want to output the structure:
foo.menu.id=file 
foo.menu.value=File 
foo.menu.popup.menuitem[0].value=New 
foo.menu.popup.menuitem[0].onclick=CreateNewDoc()
foo.menu.popup.menuitem[1].value=Open

And so forth. My use case is that it will make easier to work with JSON APIs if I can get an overview of the keys I might want to use in my mashup.
How would I achieve that in PHP or nodejs? Or some other tool? If the text value is very long then it could be truncated to be nicely lines based.

A:

Note that in most cases, you should simply parse JSON in the desired target language. If that's not an option, you can easily iterate over all elements, like this:
<?php
function unroll_obj($o, $prefix='') {
    foreach ($o as $k => $v) {
        if (is_array($v)) {
            unroll_obj($v, $prefix . $k . '.');
        } else {
            echo $prefix . $k . '=' . $v . "\n";
        }
    }
}

$s = '{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
     {"value": "Open", "onclick": "OpenDoc()"},
     {"value": "Close", "onclick": "CloseDoc()"}
   ]
 }
}}';

$o = json_decode($s, true);
unroll_obj($o);

