MQTT value transform...trigger? Or read JSON location into fake device tracker?

I have data coming in on MQTT, in JSON, which eventually I want to read as GPS coordinates with the mqtt_json integration. But, while part of the payload is properly formatted, it’s getting published inside an objectJSON property, not as the top-level message value of the topic. So for instance my data is posted on a topic:

application/1/device/0000c3a0fe41029f/event/up

but looks like this:

{
  "objectJSON": {
    "latitude": 33.333,
    "longitude": 66.666
    //...
  }
}

and I need to “unwrap” the contents and put it on another topic for mqtt_json to read. I have a NodeRED instance running elsewhere, and could probably do this transformation there, but I don’t like (if possible) to have an additional component in the critical path for the data flow.

So, is there a way to do this just within Home Assistant?

I see I can make an automation that triggers on an MQTT topic, but I don’t think within the automation I can read/transform the JSON (can I?), and then write it back to MQTT (maybe?).

I also looked briefly into creating a fake device tracker device (helper entity?) and populating its location based on the JSON, which I might be able to do with two or three automations with MQTT triggers with value_templates for the individual scalar values in the JSON, but I couldn’t see that you can make such a fake location-enabled device… and I might prefer the NodeRED transform solution to this.

I see several other people have similar problems unanswered, though they may not realize that this path will solve their problem. Anyone know how to do something like this with HA’s MQTT support and automations/scripts?

I think this will re-publish what you want:

automation:
  trigger:
    - platform: mqtt
      topic: "application/1/device/0000c3a0fe41029f/event/up"
  action: 
    - service: mqtt.publish
      data:
        topic: "the_topic_you_want_the_data_on"
        payload: "{{ trigger.payload_json["objectJSON"] }}"

That… that did it exactly. Thank you.

Interestingly…! After I wrote the above I realized that my objectJSON contents were actually a serialized string of JSON and not JSON objects, but this worked regardless… great! I can sort of understand why that would happen/work… But I fully expect this to break at some point. Even almost sounds like there could be a security issue there for someone smarter than me (automatic interpretation of a string… sounds a little like eval).