JXA: Problem converting plist with "<data>" datatype to JSON and back

@ComplexPoint (or anyone else):

Consider this action:

Here's the action's plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Action</key>
        <string>DisplayWindow</string>
        <key>MacroActionType</key>
        <string>InsertText</string>
        <key>StyledText</key>
        <data>
        cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC4jAQAAKwAAAAEAAAAb
        AQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2FydGYxNDA0XGNvY29h
        c3VicnRmNDcwCntcZm9udHRibFxmMFxmbmlsXGZjaGFyc2V0MCBIZWx2ZXRp
        Y2FOZXVlO30Ke1xjb2xvcnRibDtccmVkMjU1XGdyZWVuMjU1XGJsdWUyNTU7
        fQpccGFyZFx0eDU2MFx0eDExMjBcdHgxNjgwXHR4MjI0MFx0eDI4MDBcdHgz
        MzYwXHR4MzkyMFx0eDQ0ODBcdHg1MDQwXHR4NTYwMFx0eDYxNjBcdHg2NzIw
        XHBhcmRpcm5hdHVyYWxccGFydGlnaHRlbmZhY3RvcjAKClxmMFxmczI2IFxj
        ZjAgSGVsbG8gd29ybGQufQEAAAAjAAAAAQAAAAcAAABUWFQucnRmEAAAANqr
        VVi2AQAAAAAAAAAAAAA=
        </data>
        <key>Text</key>
        <string>Hello world.</string>
    </dict>
</array>
</plist>

Notice the <data> datatype. This datatype occurs in other places, like macros that have icons, for instance.

If I try to convert it to JSON, here's what I get:

[
    {
        "Action": "DisplayWindow",
        "MacroActionType": "InsertText",
        "Text": "Hello world.",
        "StyledText": {}
    }
]

Notice what it did to "StyledText". Do you have any thoughts on how to fix this?

I know that I could use a "replacer" function in JSON.stringify() to have <data> types stored as a string, but I don't know how to return it to <data> when I eventually convert it back to a plist (so I can paste it back into KM, for example).

Any ideas? I can think of some bastardizations, but nothing that wouldn't "smell" really badly.

I think that’s a base64 encoding of binary – there’s some Stack Overflow discussion accessible through search strings combining JSON with base64 or binary

Awesome! Just what I was looking for. Thanks!

I’m curious — how (why) is this styled text?

It’s a base64 encoding of RTF

That snippet in Dan’s example above decodes to:

rtfdaTXT.rtf.#+e{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl\f0\fnil\fcharset0 HelveticaNeue;}
{\colortbl;\red255\green255\blue255;}
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

\f0\fs26 \cf0 Hello world.}#aTXT.rtf