I don't think we can detect and remove the default values from this output in a reliable way?
This is pretty difficult - any parameter can be important, and hard to identify default values on client side without connect to server side. I don't see any other way, than hold bitmap for identification entered values and default values from the input.
That would be nice, but probably not that big a deal.
What I think would be more useful is a way to exclude the password if you didn't need it.
Good point, and maybe even exclude it by default and provide only if asked for that explicitly.
It occurs to me the most flexible thing that could be done here would be providing a libpq function that spits out JSON connection parameters and have psql turn that into a variable. It would be easy to feed that to a SQL statement and do whatever you want with it at that point, including format it to a connection URI.
Hm... but that would mean that suddenly psql would need JSON parsing capabilities and URI escaping code would have to be moved there too? So every client that links to libpq and wants to use this feature going as far as reconstructing an URI would need both of the capabilities.
Why instead of JSON not spit conninfo format, with proper escaping? That could be a separate library call, e.g. PGgetConnectionString() and a separate backslash command: \conninfo