ExifTool Forum

ExifTool => Newbies => Topic started by: iferencz on February 22, 2023, 11:07:43 PM

Title: Write json string to custom XMP tag
Post by: iferencz on February 22, 2023, 11:07:43 PM
my config file
config.config
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::XMP::Main' => {
        xmpJson => {
            SubDirectory => {
                TagTable => 'Image::ExifTool::UserDefined::xmpJson',
            },
        },
    },
);

%Image::ExifTool::UserDefined::xmpJson = (
    GROUPS => { 0 => 'XMP', 1 => 'XMP-xmpJson', 2 => 'Other' },
    NAMESPACE => { 'xmpJson' => 'http://ns.adobe.com/xmp/1.0/' },
    WRITABLE => 'string',
    Json => {
        Struct => {
            DebugJson => { Writable => 'string' },
        },
        List => 'Seq',
    },
);

my tag data
data.txt
[
    {
        DebugJson={"a":1,"b:":2}
    },
    {
        DebugJson={"a":3,"b:":4}
    }
]

My operating system is Windows, when I run
.\exiftool -config config.config "-Json<=data.txt" 1.mp4

command, he gives me error:
Warning: DebugJson is not a structure in XMP xmpJson for XMP -xmpJson:Json
Nothing to do.

If I change DebugJson to 1 and 2 respectively, the output XML data looks like this
<rdf:Description rdf:about=''
    xmlns:xmpJson='http://ns.adobe.com/xmp/1.0/'>
    <xmpJson:Json>
        <rdf:Seq>
            <rdf:li rdf:parseType='Resource'>
                <xmpJson:DebugJson>1
                </xmpJson:DebugJson>
            </rdf:li>
            <rdf:li rdf:parseType='Resource'>
                <xmpJson:DebugJson>2
                </xmpJson:DebugJson>
            </rdf:li>
        </rdf:Seq>
    </xmpJson:Json>
</rdf:Description>

I want to add json string to xmp tag, how can this be done, thank you very much! My thinking is that curly braces and commas may affect exiftool's parsing data, how to avoid it, let exiftool not parse curly braces and commas, and treat DebugJson as a Json string.
Like This
<rdf:Description rdf:about=''
    xmlns:xmpJson='http://ns.adobe.com/xmp/1.0/'>
    <xmpJson:Json>
        <rdf:Seq>
            <rdf:li rdf:parseType='Resource'>
                <xmpJson:DebugJson>{&quot;a&quot;:1,&quot;b:&quot;:2}
                </xmpJson:DebugJson>
            </rdf:li>
            <rdf:li rdf:parseType='Resource'>
                <xmpJson:DebugJson>{&quot;a&quot;:3,&quot;b:&quot;:4}
                </xmpJson:DebugJson>
            </rdf:li>
        </rdf:Seq>
    </xmpJson:Json>
</rdf:Description>
Title: Re: Write json string to custom XMP tag
Post by: Phil Harvey on February 23, 2023, 09:31:43 AM
You have explained this problem well.

Some characters must be escaped when a structure is serialized.  Read here (https://exiftool.org/struct.html#Serialize) for the serialization rules.

Specifically, your data.txt file should be:

[
    {
        DebugJson=|{"a":1|,"b:":2|}
    },
    {
        DebugJson=|{"a":3|,"b:":4|}
    }
]

- Phil
Title: Re: Write json string to custom XMP tag
Post by: iferencz on February 23, 2023, 10:31:41 AM
Quote from: Phil Harvey on February 23, 2023, 09:31:43 AMYou have explained this problem well.

Some characters must be escaped when a structure is serialized.  Read here (https://exiftool.org/struct.html#Serialize) for the serialization rules.

Specifically, your data.txt file should be:

[
    {
        DebugJson=|{"a":1|,"b:":2|}
    },
    {
        DebugJson=|{"a":3|,"b:":4|}
    }
]

- Phil
thank you! ;)
Title: Re: Write json string to custom XMP tag
Post by: Phil Harvey on February 23, 2023, 10:43:09 AM
I should have mentioned that this writes a newline and a bunch of spaces at the end of your DebugJson value, but this seems to be what you wanted.  If you change your mind, add a "," to the end of that line (after the closing brace) to avoid the extra whitespace in the output.

- Phil