Search code examples
tagscommand-line-interfacemetadatawindows-scriptingexiftool

Exiftool - Batch join tags keywords from JSON to jpg files


I have a thousands of JPG and corresponding JSON files in a Windows 10 directory, along with exiftool.exe:

0001.jpg
0001.jpg.json
0002.jpg
0002.jpg.json
0003.jpg
0003.jpg.json
...etc.
exiftool.exe

You can see the JSON file structure here:

{
    "result": {
        "tags": [
            {
                "confidence": 88.152458190918,
                "tag": {
                    "en": "smasher"
                }
            },
            {
                "confidence": 55.5320205688477,
                "tag": {
                    "en": "pretty"
                }
            },
            {
                "confidence": 50.7880249023438,
                "tag": {
                    "en": "model"
                }
            },
            {
                "confidence": 49.2356948852539,
                "tag": {
                    "en": "face"
                }
            },
            {
                "confidence": 48.7588233947754,
                "tag": {
                    "en": "portrait"
                }
            },
            {
                "confidence": 47.0902328491211,
                "tag": {
                    "en": "attractive"
                }
            },
            {
                "confidence": 43.7929306030273,
                "tag": {
                    "en": "hair"
                }
            },
            {
                "confidence": 42.2323875427246,
                "tag": {
                    "en": "adult"
                }
            },
            {
                "confidence": 41.1505165100098,
                "tag": {
                    "en": "sexy"
                }
            },
            {
                "confidence": 40.7649612426758,
                "tag": {
                    "en": "lady"
                }
            },
            {
                "confidence": 37.1722984313965,
                "tag": {
                    "en": "person"
                }
            },
            {
                "confidence": 37.1080093383789,
                "tag": {
                    "en": "fashion"
                }
            },
            {
                "confidence": 36.7636756896973,
                "tag": {
                    "en": "brunette"
                }
            },
            {
                "confidence": 36.7621879577637,
                "tag": {
                    "en": "cute"
                }
            },
            {
                "confidence": 34.9863166809082,
                "tag": {
                    "en": "skin"
                }
            },
            {
                "confidence": 32.104736328125,
                "tag": {
                    "en": "happy"
                }
            },
            {
                "confidence": 31.2583065032959,
                "tag": {
                    "en": "makeup"
                }
            },
            {
                "confidence": 29.4580879211426,
                "tag": {
                    "en": "posing"
                }
            },
            {
                "confidence": 28.6357250213623,
                "tag": {
                    "en": "smile"
                }
            },
            {
                "confidence": 28.5793495178223,
                "tag": {
                    "en": "people"
                }
            },
            {
                "confidence": 28.2635555267334,
                "tag": {
                    "en": "make"
                }
            },
            {
                "confidence": 27.4844341278076,
                "tag": {
                    "en": "studio"
                }
            },
            {
                "confidence": 27.2048511505127,
                "tag": {
                    "en": "cover girl"
                }
            },
            {
                "confidence": 25.5794258117676,
                "tag": {
                    "en": "sensual"
                }
            },
            {
                "confidence": 25.0063171386719,
                "tag": {
                    "en": "lovely"
                }
            },
            {
                "confidence": 24.7523536682129,
                "tag": {
                    "en": "one"
                }
            },
            {
                "confidence": 24.2116451263428,
                "tag": {
                    "en": "eyes"
                }
            },
            {
                "confidence": 24.2004241943359,
                "tag": {
                    "en": "lock"
                }
            },
            {
                "confidence": 23.7295970916748,
                "tag": {
                    "en": "sensuality"
                }
            },
            {
                "confidence": 23.2414417266846,
                "tag": {
                    "en": "lips"
                }
            },
            {
                "confidence": 22.531270980835,
                "tag": {
                    "en": "smiling"
                }
            },
            {
                "confidence": 22.2798938751221,
                "tag": {
                    "en": "expression"
                }
            },
            {
                "confidence": 21.9316215515137,
                "tag": {
                    "en": "elegance"
                }
            },
            {
                "confidence": 21.450065612793,
                "tag": {
                    "en": "women"
                }
            },
            {
                "confidence": 20.9808216094971,
                "tag": {
                    "en": "closeup"
                }
            },
            {
                "confidence": 20.94065284729,
                "tag": {
                    "en": "gorgeous"
                }
            },
            {
                "confidence": 20.8894214630127,
                "tag": {
                    "en": "looking"
                }
            },
            {
                "confidence": 19.6783428192139,
                "tag": {
                    "en": "youth"
                }
            },
            {
                "confidence": 19.4935684204102,
                "tag": {
                    "en": "natural"
                }
            },
            {
                "confidence": 19.0460243225098,
                "tag": {
                    "en": "fresh"
                }
            },
            {
                "confidence": 18.9859828948975,
                "tag": {
                    "en": "healthy"
                }
            },
            {
                "confidence": 18.8311977386475,
                "tag": {
                    "en": "human"
                }
            },
            {
                "confidence": 17.8766403198242,
                "tag": {
                    "en": "casual"
                }
            },
            {
                "confidence": 17.6684837341309,
                "tag": {
                    "en": "body"
                }
            },
            {
                "confidence": 17.5176486968994,
                "tag": {
                    "en": "long"
                }
            },
            {
                "confidence": 16.7830467224121,
                "tag": {
                    "en": "clean"
                }
            },
            {
                "confidence": 16.5375003814697,
                "tag": {
                    "en": "care"
                }
            },
            {
                "confidence": 16.2804470062256,
                "tag": {
                    "en": "hairstyle"
                }
            },
            {
                "confidence": 16.0539932250977,
                "tag": {
                    "en": "health"
                }
            },
            {
                "confidence": 15.838433265686,
                "tag": {
                    "en": "look"
                }
            },
            {
                "confidence": 15.6484327316284,
                "tag": {
                    "en": "style"
                }
            },
            {
                "confidence": 14.9056119918823,
                "tag": {
                    "en": "close"
                }
            },
            {
                "confidence": 14.3418111801147,
                "tag": {
                    "en": "head"
                }
            },
            {
                "confidence": 14.0835266113281,
                "tag": {
                    "en": "modern"
                }
            },
            {
                "confidence": 13.8814821243286,
                "tag": {
                    "en": "cheerful"
                }
            },
            {
                "confidence": 13.8072738647461,
                "tag": {
                    "en": "20s"
                }
            },
            {
                "confidence": 13.6136884689331,
                "tag": {
                    "en": "dress"
                }
            },
            {
                "confidence": 13.0692186355591,
                "tag": {
                    "en": "lifestyle"
                }
            },
            {
                "confidence": 12.909481048584,
                "tag": {
                    "en": "elegant"
                }
            },
            {
                "confidence": 12.8674716949463,
                "tag": {
                    "en": "friendly"
                }
            },
            {
                "confidence": 12.8258485794067,
                "tag": {
                    "en": "teenager"
                }
            },
            {
                "confidence": 12.7428483963013,
                "tag": {
                    "en": "pose"
                }
            },
            {
                "confidence": 11.9529628753662,
                "tag": {
                    "en": "wellness"
                }
            },
            {
                "confidence": 11.2883729934692,
                "tag": {
                    "en": "cosmetics"
                }
            },
            {
                "confidence": 11.1015768051147,
                "tag": {
                    "en": "slim"
                }
            },
            {
                "confidence": 10.9321374893188,
                "tag": {
                    "en": "hot"
                }
            },
            {
                "confidence": 10.5805339813232,
                "tag": {
                    "en": "cosmetic"
                }
            },
            {
                "confidence": 10.5062713623047,
                "tag": {
                    "en": "skincare"
                }
            },
            {
                "confidence": 10.3531084060669,
                "tag": {
                    "en": "brown"
                }
            },
            {
                "confidence": 10.2322235107422,
                "tag": {
                    "en": "happiness"
                }
            },
            {
                "confidence": 10.1532421112061,
                "tag": {
                    "en": "teen"
                }
            },
            {
                "confidence": 9.91652774810791,
                "tag": {
                    "en": "spa"
                }
            },
            {
                "confidence": 9.77626037597656,
                "tag": {
                    "en": "shoulder"
                }
            },
            {
                "confidence": 9.64915561676025,
                "tag": {
                    "en": "vertical"
                }
            },
            {
                "confidence": 9.56869029998779,
                "tag": {
                    "en": "black"
                }
            },
            {
                "confidence": 9.28471755981445,
                "tag": {
                    "en": "purity"
                }
            },
            {
                "confidence": 9.20380115509033,
                "tag": {
                    "en": "blond"
                }
            },
            {
                "confidence": 9.08255577087402,
                "tag": {
                    "en": "stylish"
                }
            },
            {
                "confidence": 8.62055492401123,
                "tag": {
                    "en": "bright"
                }
            },
            {
                "confidence": 7.54396390914917,
                "tag": {
                    "en": "wellbeing"
                }
            },
            {
                "confidence": 7.4316349029541,
                "tag": {
                    "en": "single"
                }
            },
            {
                "confidence": 7.382728099823,
                "tag": {
                    "en": "treatment"
                }
            },
            {
                "confidence": 7.36639595031738,
                "tag": {
                    "en": "nice"
                }
            },
            {
                "confidence": 7.34033250808716,
                "tag": {
                    "en": "alone"
                }
            }
        ]
    },
    "status": {
        "text": "",
        "type": "success"
    }
}

I want to take all Tags in each .jpg.json file into the metadata of the .jpg file.

I have searched a lot but haven't been able to solve the problem.


Solution

  • Assuming you want to copy the "tag" entries into a keyword list type tag, the basic command would be

    exiftool -TagsFromFile %d%F.json "-XMP-dc:Subject<TAG" /path/to/files/
    

    The problem is figuring out what you need to use for TAG. To do so, you would run the command in Exiftool FAQ #3. The output from that command would include this

    [JSON]          ResultTagsTagEn                 : smasher, pretty, model, <…snip>
    

    This shows that you want to replace TAG in the above command with ResultTagsTagEn, e.g.

    exiftool -TagsFromFile %d%F.json "-XMP-dc:Subject<ResultTagsTagEn" /path/to/files/
    

    The command is not complete yet, as this command would create a single keyword that would have all the tag values in it. In other words, it would create a single keyword of

    "smasher, pretty, model, <…snip>"
    

    not the desired result of

    "smasher"
    "pretty"
    "model"
    <…snip>
    

    To fix this, the -sep option must be used to separate the individual keywords. The output of ResultTagsTagEn shows the tags are split by ", " (CommaSpace).

    Adding that option in results in

    exiftool -sep ", " -TagsFromFile %d%F.json "-XMP-dc:Subject<ResultTagsTagEn" /path/to/files/
    

    This command will remove any previously existing keywords in favor of the ones in the JSON file. If you want to add keywords and keep the originals, you would put a plus sign before the less than sign

    exiftool -sep ", " -TagsFromFile %d%F.json "-XMP-dc:Subject+<ResultTagsTagEn" /path/to/files/
    

    XMP-dc:Subject is the most modern way to include keywords in a file, but some older programs may not read it. In that case, you would also want to write to the IPTC IIM/Legacy tag as well

    exiftool -sep ", " -TagsFromFile %d%F.json "-XMP-dc:Subject+<ResultTagsTagEn" "-IPTC:Keywords+<ResultTagsTagEn" /path/to/files/