Getting topic inside the mqtt json object

I am using hivemqtt cli.

If I subscribe to a topic with sitename/# I get all messages across all topics. Is there a way I get the topic corresponding to the message object?

Here is my client on command line:
mqtt sub -h -t hmplant/# >> new_sub.txt&

The output comes in each line like this:

{“TS”:“1668685509”, “ID”:“3”, “ST”:“149”, “VR”: “[2.128884]”}
{“TS”:“1668685539”, “ID”:“3”, “ST”:“153”, “VR”: “[1.504379]”}
{“TS”:“1668685539”, “ID”:“3”, “ST”:“155”, “VR”: “[4.439028]”}
{“TS”:“1668685539”, “ID”:“3”, “ST”:“165”, “VR”: “[-1388.689087]”}

There is no indication of the topic against which each message came in.

I would like to get the topic either inside the json object or outside.

Hi @sanjmeh ,

Did you try using mqtt subscribe with --showTopics option:

  -T, --showTopics        Prepend the specific topic name to the received publish

you might also find interesting the --jsonOutput option:

  -J, --jsonOutput        Print the received publishes in pretty JSON format

If you cannot find particular option in the Subscribe - MQTT CLI, you can always see the list of available options by running:

mqtt subscribe --help

I hope this helps,
Dasha from HiveMQ team

Hi Daria, Thanks for the quick response.

I think the -T option does not exist. On checking the following options exist.

Usage:  mqtt sub [-cdsv] [-b64] [--help] [-oc] [--[no-]reqProblemInfo] [--[no-]reqResponseInfo] [--version] [-Wr] [-pw [<password>]] [--cert
                 <clientCertificate>] [-h <host>] [-i <identifier>] [-ip <identifierPrefix>] [-k <keepAlive>] [--key <clientPrivateKey>] [--maxPacketSize
                 <maximumPacketSize>] [-of <receivedMessagesFile>] [-p <port>] [--rcvMax <receiveMaximum>] [-se <sessionExpiryInterval>] [--sendMax
                 <sendMaximum>] [--sendMaxPacketSize <sendMaximumPacketSize>] [--sendTopicAliasMax <sendTopicAliasMaximum>] [--topicAliasMax
                 <topicAliasMaximum>] [-u <user>] [-V <version>] [-Wcd <willCorrelationData>] [-Wct <willContentType>] [-Wd <willDelayInterval>] [-We
                 <willMessageExpiryInterval>] [-Wm <willMessage>] [-Wpf <willPayloadFormatIndicator>] [-Wq <willQos>] [-Wrt <willResponseTopic>] [-Wt
                 <willTopic>] [--cafile FILE]... [--capath DIR]... [--ciphers <cipherSuites>[:<cipherSuites>...]]... [-Cup <connectUserProperties>]... [-q
                 <qos>]... -t <topics> [-t <topics>]... [--tls-version <supportedTLSVersions>]... [-up <userProperties>]... [-Wup <willUserProperties>]...

Finally, I notice it is a different MQTT client than HiveMQ.

$ mqtt -V
Picocli 4.0.4
JVM: 11.0.17 (Ubuntu OpenJDK 64-Bit Server VM 11.0.17+8-post-Ubuntu-1ubuntu220.04)
OS: Linux 5.4.0-107-generic amd64

I did install using the instructions given here:

$ wget
$ sudo apt install ./mqtt-cli_1.0.0_all.deb

UPDATE after more experimenting:
I got the topic with the following command line subscription:

mqtt sub -i term4 -h -t hmplant/#  -of outfile.txt &

SImply saving in an external file, automatically added the topic prefix in each message.

@sanjmeh ,

you need to update your mqtt-cli. I have version 4.9.1:

#mqtt --version
Picocli 4.6.3
JVM: (Homebrew OpenJDK 64-Bit Server VM
OS: Mac OS X 13.0.1 x86_64

Dasha from HiveMQ team