Page 1 of 1

mpc output format - escape quotes

Posted: August 8th, 2019, 4:27 pm
by nnet
Hi, I'm trying to create json output using mpc:

Code: Select all

mpc -f '{"artist": "%artist%",["album": "%album%",]"title": "%title%",["track_number": "%track%",]"duration": "%time%"}' search artist savatage
produces output:

Code: Select all

{"artist": "Savatage","album": ""Streets" A Rock Opera","title": "Believe","track_number": "16","duration": "5:42"}
Since json doesn't permit single or double quotes without being escaped, I was attempting to use # as shown in mpc man page. I must be using it incorrectly since it's not working:

Code: Select all

The [] operator is used to group output such that if no metadata delimiters are found or matched
between [ and ], then none of the characters between [ and ] are output.  & and  |  are  logical
operators for and and or.  # is used to escape characters.  Some useful examples for format are:
"%file%" and "[[%artist% - ]%title%]|[%file%]".  This command also takes the  following  defined
escape sequences:
Can anyone explain how to use # to escape quote chars in a field?

Thanks

Re: mpc output format - escape quotes

Posted: August 8th, 2019, 5:53 pm
by max
nnet wrote:
August 8th, 2019, 4:27 pm
Can anyone explain how to use # to escape quote chars in a field?
Not at all. mpc never quotes its output. The "#" may be used to quote special characters in the format specification.

If you want to quote output values, mpc is the wrong tool for the job. Better write a Python two-liner using a MPD client library and a JSON formatting library.

[SOLVED] Re: mpc output format - escape quotes

Posted: August 8th, 2019, 7:37 pm
by nnet
Ah ok, I misunderstood its use then, it doesn't filter output, it provides a means of escaping arbitrary characters supplied in the format template.
Thanks for the response and explanation!