<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extracting a value from MQTT string before parsing to JSON in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/745043#M118438</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/183155"&gt;@luminousplumz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to apply the mqtttopic transform &lt;EM&gt;before&lt;/EM&gt; the mqtttojson transform overwrites the _raw field. The order in TRANSFORMS-* matters. Also, adjust the mqtttopic regex and format for correct field extraction.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;transforms.conf:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[mqtttojson]
REGEX = msg\=(.+)
FORMAT = $1
DEST_KEY = _raw

[mqtttopic]
# Extract from the original _raw field containing 'topic='
REGEX = topic=tgw\/data\/0x155f\/([^\/]+)
FORMAT = Topic::$1
WRITE_META = true&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;props.conf:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[mqtttojson_ubnpfc_all]
# Apply mqtttopic first, then mqtttojson
TRANSFORMS-topic_then_json = mqtttopic, mqtttojson
# The rest of your props.conf settings remain the same
DATETIME_CONFIG =
LINE_BREAKER = ([\r\n]+)
NO_BINARY_CHECK = true
TIME_PREFIX = \"ts\":
TZ = Europe/London
category = Custom
pulldown_type = 1
# Ensure KV_MODE=none if you don't want Splunk's default key-value extraction
# KV_MODE = none
# Ensure JSON extraction runs after transforms if needed
# INDEXED_EXTRACTIONS = json&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;Transform Order:&lt;/STRONG&gt; The TRANSFORMS-topic_then_json line in props.conf&amp;nbsp;should have&amp;nbsp;mqtttopic first. This ensures it runs on the original event data &lt;EM&gt;before&lt;/EM&gt; mqtttojson overwrites _raw with the JSON payload.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic REGEX:&lt;/STRONG&gt; The regex topic=tgw\/data\/0x155f\/([^\/]+) specifically looks for the topic= string, skips the known prefix tgw/data/0x155f/, and captures the next segment of characters that are not a forward slash (/) into capture group 1.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic FORMAT:&lt;/STRONG&gt; FORMAT = Topic::$1 creates a new field named Topic containing the value captured by the regex (the desired topic segment, e.g., "TransportContextTracking").&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic WRITE_META:&lt;/STRONG&gt; WRITE_META = true ensures the extracted field (Topic) is written to the index metadata, making it available for searching even though the original _raw field is later overwritten.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttojson:&lt;/STRONG&gt; This transform runs second. It extracts the JSON part from the msg= field (which still exists in the original event data at this stage) and overwrites _raw with just the JSON content. Splunk's automatic JSON parsing (or INDEXED_EXTRACTIONS = json) will then parse this new _raw.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;Some useful tips:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Restart the Splunk instance or reload the configuration for changes in props.conf and transforms.conf to take effect.&lt;/LI&gt;&lt;LI&gt;Ensure the sourcetype mqtttojson_ubnpfc_all is correctly assigned to your MQTT data input.&lt;/LI&gt;&lt;LI&gt;Test the regex using Splunk's rex command in search or on regex testing websites against your raw event data to confirm it captures the correct value.&lt;/LI&gt;&lt;LI&gt;If Splunk's automatic key-value extraction interferes before your transforms run, you might need KV_MODE = none in props.conf.&lt;/LI&gt;&lt;LI&gt;If Splunk isn't automatically parsing the final JSON _raw, add INDEXED_EXTRACTIONS = json to your props.conf stanza.&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":glowing_star:"&gt;🌟&lt;/span&gt; &lt;STRONG&gt;Did this answer help you?&lt;/STRONG&gt; If so, please consider:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Adding karma to show it was useful&lt;/LI&gt;&lt;LI&gt;Marking it as the solution if it resolved your issue&lt;/LI&gt;&lt;LI&gt;Commenting if you need any clarification&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Your feedback encourages the volunteers in this community to continue contributing&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Sat, 26 Apr 2025 20:52:34 GMT</pubDate>
    <dc:creator>livehybrid</dc:creator>
    <dc:date>2025-04-26T20:52:34Z</dc:date>
    <item>
      <title>Extracting a value from MQTT string before parsing to JSON</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/744909#M118416</link>
      <description>&lt;P&gt;I have an requirement to extract a value from an mqtt string before i parse it to json.&lt;BR /&gt;Initially i was using MQTT Modular input app to pull each of the topics with their own input.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found that with more than 3 inputs /topics enabled i am dropping some if not all data.&lt;BR /&gt;So i decided to pull all the topics in a single input. This works well except i still need to be able to separate the topics for searches.&lt;/P&gt;&lt;P&gt;I managed to get this working using multiple transforms. i changed something and now i can get it to work again.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Using Transforms i can parse to json with no issues (mqtttojson)&lt;/P&gt;&lt;P&gt;Transforms.conf&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;[mqtttojson]&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;REGEX&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;=&amp;nbsp;msg\=(.+)$&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;FORMAT&lt;/S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PAN&gt;&lt;SPAN&gt;mqtttopic&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;mqtttojson,&amp;nbsp;mqtttopic&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;In the example below i need the 4th topic level i.e. "TransportContextTracking".&lt;BR /&gt;&lt;BR /&gt;Thu Apr 24 12:42:15 GMT 2025 name=mqtt_msg_received event_id= topic=tgw/data/0x155f/TransportContextTracking/MFC/0278494 msg={"data":{"destination":{"locationAddress":"/UrbanUK/PCOTS13/Exit"},"errorCode":null,"event":"Started","loadCarrierId":"0278494","source":{"locationAddress":"/UrbanUK/PCOTS13/Pick"},"transportId":"f0409b2a-e9d4-407c-bd65-48ccea17b520","transportType":"Transport"},"dbid":8104562815,"ts":1745498528217}&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am i missing ?????&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2025 14:05:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/744909#M118416</guid>
      <dc:creator>luminousplumz</dc:creator>
      <dc:date>2025-04-24T14:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting a value from MQTT string before parsing to JSON</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/745040#M118437</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/183155"&gt;@luminousplumz&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;For &lt;STRONG&gt;index-time&lt;/STRONG&gt; field extractions, you want something like this (note the &lt;EM&gt;order of the transforms&lt;/EM&gt; in the TRANSFORMS-mqtt setting):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;# fields.conf

[sourcetype::mqtttojson_ubnpfc_all::Topic]
INDEXED = true

# props.conf

[mqtttojson_ubnpfc_all]
TRANSFORMS-mqtt = mqtttopic,mqtttojson

# transforms.conf

[mqtttojson]
CLEAN_KEYS = 0
DEST_KEY = _raw
FORMAT = $1
REGEX = msg=(.+)

[mqtttopic]
CLEAN_KEYS = 0
FORMAT = Topic::$1
REGEX = topic=(?:[^/]*/){3}([^/]+)
WRITE_META = true&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;For&amp;nbsp;&lt;STRONG&gt;search-time&lt;/STRONG&gt; field extractions, you want something like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[mqtttojson_ubnpfc_all]
EXTRACT-Topic = topic=(?:[^/]*/){3}(?&amp;lt;Topic&amp;gt;[^/]+)
EVAL-_raw = replace(_raw, ".*? msg=", "")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;However, in the search-time configuration, you'll need to extract the JSON fields in a search as automatic key-value field extraction happens before calculated fields (EVAL-*):&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;sourcetype=mqtttojson_ubnpfc_all
| spath&lt;/LI-CODE&gt;&lt;P&gt;You'll note that the original name, event_id, topic, and msg (value possibly truncated) fields are automatically extracted before the full value of msg is assigned to _raw.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Apr 2025 17:21:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/745040#M118437</guid>
      <dc:creator>tscroggins</dc:creator>
      <dc:date>2025-04-26T17:21:23Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting a value from MQTT string before parsing to JSON</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/745043#M118438</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/183155"&gt;@luminousplumz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to apply the mqtttopic transform &lt;EM&gt;before&lt;/EM&gt; the mqtttojson transform overwrites the _raw field. The order in TRANSFORMS-* matters. Also, adjust the mqtttopic regex and format for correct field extraction.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;transforms.conf:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[mqtttojson]
REGEX = msg\=(.+)
FORMAT = $1
DEST_KEY = _raw

[mqtttopic]
# Extract from the original _raw field containing 'topic='
REGEX = topic=tgw\/data\/0x155f\/([^\/]+)
FORMAT = Topic::$1
WRITE_META = true&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;props.conf:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[mqtttojson_ubnpfc_all]
# Apply mqtttopic first, then mqtttojson
TRANSFORMS-topic_then_json = mqtttopic, mqtttojson
# The rest of your props.conf settings remain the same
DATETIME_CONFIG =
LINE_BREAKER = ([\r\n]+)
NO_BINARY_CHECK = true
TIME_PREFIX = \"ts\":
TZ = Europe/London
category = Custom
pulldown_type = 1
# Ensure KV_MODE=none if you don't want Splunk's default key-value extraction
# KV_MODE = none
# Ensure JSON extraction runs after transforms if needed
# INDEXED_EXTRACTIONS = json&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;Transform Order:&lt;/STRONG&gt; The TRANSFORMS-topic_then_json line in props.conf&amp;nbsp;should have&amp;nbsp;mqtttopic first. This ensures it runs on the original event data &lt;EM&gt;before&lt;/EM&gt; mqtttojson overwrites _raw with the JSON payload.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic REGEX:&lt;/STRONG&gt; The regex topic=tgw\/data\/0x155f\/([^\/]+) specifically looks for the topic= string, skips the known prefix tgw/data/0x155f/, and captures the next segment of characters that are not a forward slash (/) into capture group 1.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic FORMAT:&lt;/STRONG&gt; FORMAT = Topic::$1 creates a new field named Topic containing the value captured by the regex (the desired topic segment, e.g., "TransportContextTracking").&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttopic WRITE_META:&lt;/STRONG&gt; WRITE_META = true ensures the extracted field (Topic) is written to the index metadata, making it available for searching even though the original _raw field is later overwritten.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;mqtttojson:&lt;/STRONG&gt; This transform runs second. It extracts the JSON part from the msg= field (which still exists in the original event data at this stage) and overwrites _raw with just the JSON content. Splunk's automatic JSON parsing (or INDEXED_EXTRACTIONS = json) will then parse this new _raw.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;Some useful tips:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Restart the Splunk instance or reload the configuration for changes in props.conf and transforms.conf to take effect.&lt;/LI&gt;&lt;LI&gt;Ensure the sourcetype mqtttojson_ubnpfc_all is correctly assigned to your MQTT data input.&lt;/LI&gt;&lt;LI&gt;Test the regex using Splunk's rex command in search or on regex testing websites against your raw event data to confirm it captures the correct value.&lt;/LI&gt;&lt;LI&gt;If Splunk's automatic key-value extraction interferes before your transforms run, you might need KV_MODE = none in props.conf.&lt;/LI&gt;&lt;LI&gt;If Splunk isn't automatically parsing the final JSON _raw, add INDEXED_EXTRACTIONS = json to your props.conf stanza.&lt;/LI&gt;&lt;/UL&gt;&lt;DIV&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":glowing_star:"&gt;🌟&lt;/span&gt; &lt;STRONG&gt;Did this answer help you?&lt;/STRONG&gt; If so, please consider:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Adding karma to show it was useful&lt;/LI&gt;&lt;LI&gt;Marking it as the solution if it resolved your issue&lt;/LI&gt;&lt;LI&gt;Commenting if you need any clarification&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Your feedback encourages the volunteers in this community to continue contributing&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 26 Apr 2025 20:52:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-a-value-from-MQTT-string-before-parsing-to-JSON/m-p/745043#M118438</guid>
      <dc:creator>livehybrid</dc:creator>
      <dc:date>2025-04-26T20:52:34Z</dc:date>
    </item>
  </channel>
</rss>

