mpc output format - escape quotes

There are many, many clients for MPD. Many of them are old and unsupported, but maybe someone here can help you with them anyway...
Post Reply
nnet
Posts: 2
Joined: August 8th, 2019, 4:09 pm

mpc output format - escape quotes

Post by nnet » August 8th, 2019, 4:27 pm

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

max
Forum team
Posts: 958
Joined: January 15th, 2013, 3:43 pm

Re: mpc output format - escape quotes

Post by max » August 8th, 2019, 5:53 pm

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.

nnet
Posts: 2
Joined: August 8th, 2019, 4:09 pm

[SOLVED] Re: mpc output format - escape quotes

Post by nnet » August 8th, 2019, 7:37 pm

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!

Post Reply