# Getting topic inside the mqtt json object

**URL:** <https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608>\
**Category:** Uncategorized\
**Created:** [November 17, 2022, 12:07pm UTC](https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608 "2022-11-17T12:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjmeh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/sanjmeh/32/305_2.png) [@sanjmeh](https://community.hivemq.com/u/sanjmeh)\
**Post date:** [November 17, 2022, 12:07pm UTC](https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608/1 "2022-11-17T12:07:26Z")

</div>

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 xxxxx.net -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.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [November 17, 2022, 12:32pm UTC](https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608/2 "2022-11-17T12:32:36Z")

</div>

Hi @sanjmeh ,

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

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

```

you might also find interesting the `--jsonOutput` option:

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

```

If you cannot find particular option in the [Subscribe - MQTT CLI](https://hivemq.github.io/mqtt-cli/docs/subscribe/), you can always see the list of available options by running:

```auto
mqtt subscribe --help

```

I hope this helps,  
Best,  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![sanjmeh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/sanjmeh/32/305_2.png) [@sanjmeh](https://community.hivemq.com/u/sanjmeh)\
**Post date:** [November 17, 2022, 2:57pm UTC](https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608/3 "2022-11-17T14:57:55Z")

</div>

> [@Daria\_H](#):
>
> `mqtt subscribe --help`

Hi Daria, Thanks for the quick response.

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

```auto
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.

```auto
$ mqtt -V
1.0.0
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:

```auto
$ wget 
https://github.com/hivemq/mqtt-cli/releases/download/v1.0.0/mqtt-cli_1.0.0_all.deb
$ sudo apt install ./mqtt-cli_1.0.0_all.deb

```

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

```auto
mqtt sub -i term4 -h XXX.net -t hmplant/# -of outfile.txt &

```

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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [November 17, 2022, 5:23pm UTC](https://community.hivemq.com/t/getting-topic-inside-the-mqtt-json-object/1608/4 "2022-11-17T17:23:37Z")

</div>

@sanjmeh ,

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

```auto
#mqtt --version
4.9.1
Picocli 4.6.3
JVM: 11.0.16.1 (Homebrew OpenJDK 64-Bit Server VM 11.0.16.1+0)
OS: Mac OS X 13.0.1 x86_64

```

Best,  
Dasha from HiveMQ team
