Flatten a nested config file
A deployment tool keeps settings as nested JSON, but the runtime wants one flat line per setting. Read a JSON object and print every leaf value as path=value, where the path joins the keys from the root with dots. Lists use their zero-based index as the key. Print true, false and null for those JSON values, and numbers and strings as they are. Keep the order in which the values appear. An empty object or an empty list produces no line at all.
Input
One line: a valid JSON object of up to 5000 characters and at most 20 levels deep. Keys contain only letters, digits and underscores. String values contain no newlines. Numbers are integers or decimals written without trailing zeros.
Output
One line per leaf value in input order, path=value. Nothing is printed for an empty object.
Example 1
Input
{"app":{"name":"ledger","debug":false},"ports":[8080,8443],"owner":null}Output
app.name=ledger app.debug=false ports.0=8080 ports.1=8443 owner=null
Nested object keys are joined with dots, list items get their index, and false and null print as words.
Example 2
Input
{"list":[[1,2],[3]],"empty":{},"e2":[],"after":"x"}Output
list.0.0=1 list.0.1=2 list.1.0=3 after=x
Lists nest like objects. The empty object and the empty list contribute nothing, and the value after them is still printed.
Example 3
Input
{}Output
(nothing)An empty object has no leaves, so the output is empty.
Constraints
- Up to 5000 characters of JSON
- Nesting depth at most 20
- Keys use letters, digits and underscores only
Hints
Hint 1 of 3
json_decode($text, true) turns both objects and lists into PHP arrays, and it keeps the key order.
Hint 2 of 3
Write flatten($value, $prefix): if is_array($value), loop over its key => child pairs and call flatten again with a longer prefix; otherwise print the line.
Hint 3 of 3
The prefix for a child is the key itself at the root and "$prefix.$key" below it. Booleans need $v ? 'true' : 'false' because echo false prints nothing.
Solution
Show a reference solution and explanation
<?php
$config = json_decode(trim(fgets(STDIN)), true);
function flatten(mixed $value, string $prefix): void
{
if (is_array($value)) {
foreach ($value as $key => $child) {
$path = $prefix === '' ? (string) $key : "$prefix.$key";
flatten($child, $path);
}
return;
}
if (is_bool($value)) {
$text = $value ? 'true' : 'false';
} elseif ($value === null) {
$text = 'null';
} else {
$text = (string) $value;
}
echo "$prefix=$text\n";
}
flatten($config, '');
Why it works
Recursion fits because a config value is either a leaf or a container of more values with the same rule applied to each. Decoding with the second argument true makes objects and lists both plain PHP arrays, so one is_array() check covers both, and PHP arrays keep insertion order, which preserves the file's order without extra work. The two conversions that need care are booleans, which echo would print as 1 or nothing, and null, which prints as nothing; both are handled explicitly. Empty containers fall out naturally: the foreach over an empty array runs zero times, so no line appears. Depth is bounded at 20, well within PHP's call stack, but the same idea would also work iteratively with an explicit stack of value and prefix pairs.
Lesson for this exercise: JSON and Data Handling in PHP