ExifTool Forum

ExifTool => Newbies => Topic started by: charlesr on June 02, 2021, 05:28:15 PM

Title: Sidecar JSON to JPG meta with keywords and file name change
Post by: charlesr on June 02, 2021, 05:28:15 PM
I have thousands of Instagram photos that are referenced by a sidecar JSON file in the following format:

  {
    "media": [
      {
        "SourceFile": "media/posts/201812/46755186_2014789298609853_5551610301785678699_n_17846292439316922.jpg",
        "DateTimeOriginal": 1545149174,
        "media_metadata": {
          "photo_metadata": {
            "exif_data": [
              {
                "latitude": -8.058,
                "longitude": 114.242
              }
            ]
          }
        },
        "Description": "When a kid is learning to walk and falls down 50 times they don\u00e2\u0080\u0099t think \u00e2\u0080\u009cnah this isn\u00e2\u0080\u0099t for me\u00e2\u0080\u009d -\n\nSo why do we have that mentality with things we were made to do. You were made to walk and didn\u00e2\u0080\u0099t give up, don\u00e2\u0080\u0099t give up on the big picture. -\n\nBe blessed and stay stoked\u00f0\u009f\u0098\u008e\u00f0\u009f\u00a4\u0099 \u00e2\u0080\u00a2\n\u00e2\u0080\u00a2\n\u00e2\u0080\u00a2\n\u00e2\u0080\u00a2\n\u00e2\u0080\u00a2\n\u00e2\u0080\u00a2\n#beautifuldestination #travelgram #adventureawaits #getlost #beautifuldestinations #openmyworld #welltravelled  #exploretheworld #passionpassport #travelersnotebook #adventureculture #ig_travel  #lifeofadventure  #liveauthentic #hawaiilife #lethawaiihappen #nakedhawaii #hawaii #hawaiiunchained #hawaiistagram #aloha #oahu #dronesdaily #droneoftheday #dronestagram #dronelife #dronefly #dronegear #dronephotography #dji"
      }
    ]
  },

I'd like to do 3 things:
1. Use the JSON file to add the metadata (description is the caption, hashtags are the keywords, photo capture date, and location) to each individual photo (there are thousands, all referenced in a single JSON file)
2. Rename the file to be "AlohaFilms_<capturedate>_<location>.jpg"
3. Create a new folder and add all the new files to that folder

I'm a newbie and can't figure out what command lines to use for this. Thanks in advance!
Charles

Mac OS 11.2.3
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: Phil Harvey on June 02, 2021, 08:49:21 PM
Hi Charles,

There are a number of posts about copying metadata from JSON files.  Here is one where StarGeek explains a few things. (https://exiftool.org/forum/index.php?topic=11072.msg59178#msg59178)

- Phil
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: charlesr on June 02, 2021, 09:25:32 PM
Thanks for the reply, Phil. I updated the JSON example above with a more recent file (sorry for the confusion).
That was one of the threads I've read already... including one you wrote about find and replace inside the JSON file (which I just finished doing).

However, I'm still having trouble referencing the JSON file - running the program returns "No file specified". Also, parsing the # out from the description is super confusing to me.

exiftool -json="Macintosh HD\Users\alohafilms\Documents\BiteSlice\Data\Instagram Data\Test Nash\posts_1.json"

Also, I was hoping that there was one single command that I could do? Instead of multiple steps?
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: StarGeek on June 03, 2021, 12:17:28 AM
The -j (-json) option (https://exiftool.org/exiftool_pod.html#j-JSONFILE--json) is used to embed or list data from other filetypes in json format.  You want to run exiftool directly on the json file to get the json tag names

exiftool -g1 -a -s 'Macintosh HD\Users\alohafilms\Documents\BiteSlice\Data\Instagram Data\Test Nash\posts_1.json' 

Running this on your example gives
---- JSON ----
MediaDateTimeOriginal           : 1545149174
MediaDescription                : When a kid is learning to walk and falls down 50 times they don’t think “nah this isn’t for me” -..So why do we have that mentality with things we were made to do. You were made to walk and didn’t give up, don’t give up on the big picture. -..Be blessed and stay stoked😎🤙 •.•.•.•.•.•.#beautifuldestination #travelgram #adventureawaits #getlost #beautifuldestinations #openmyworld #welltravelled  #exploretheworld #passionpassport #travelersnotebook #adventureculture #ig_travel  #lifeofadventure  #liveauthentic #hawaiilife #lethawaiihappen #nakedhawaii #hawaii #hawaiiunchained #hawaiistagram #aloha #oahu #dronesdaily #droneoftheday #dronestagram #dronelife #dronefly #dronegear #dronephotography #dji
MediaSourceFile                 : media/posts/201812/46755186_2014789298609853_5551610301785678699_n_17846292439316922.jpg
MediaMedia_metadataPhoto_metadataExif_dataLatitude: -8.058
MediaMedia_metadataPhoto_metadataExif_dataLongitude: 114.242


The MediaDateTimeOriginal appears to be unix time and translates to December 18, 2018 4:06:14 PM.  This may be off by the time zone as many media sites save this in UTC.

So your command would be along these lines, assuming the json files have the same base name as the jpegs
exiftool -d "%s" -TagsFromFile %d%f.json '-AllDates<MediaDateTimeOriginal' '-Description<MediaDescription' '-GPSLatitude*<MediaMedia_metadataPhoto_metadataExif_dataLatitude' '-GPSLongitude*<MediaMedia_metadataPhoto_metadataExif_dataLongitude' /path/to/files/
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: charlesr on June 03, 2021, 09:07:48 PM
Thanks, StarGeek!

There's just one single JSON file for all the photos... how would that change the command?
Also, do you know a way to parse out the #hashtags from the description into separate keywords inside the jpg?
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: StarGeek on June 03, 2021, 10:13:04 PM
Quote from: charlesr on June 03, 2021, 09:07:48 PM
There's just one single JSON file for all the photos... how would that change the command?

That makes it pretty close to impossible without some sort of editing of the json file or basically embedding a full Perl parsing script into the command.  Can you make a larger portion of the json available, like 4-5 file groupings?  It might need some other program to split the json file up.  Given a better example, I'd do some testing with jq (https://stedolan.github.io/jq/) or a similar program.  I found this StackOverflow answer (https://stackoverflow.com/questions/49808581/using-jq-how-can-i-split-a-very-large-json-file-into-multiple-files-each-a-spec) which looks promising.

QuoteAlso, do you know a way to parse out the #hashtags from the description into separate keywords inside the jpg?

That would be something like this:
exiftool -sep "," -TagsFromFile file.json "-Subject<${MediaDescription;my @matches = ( m/(#[^\s]+)/g );$_=join(',',@matches)}" file.jpg
But that won't work as long as all the data is in a single json.  Also, I haven't tested it on case where there are no hashtags.
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: charlesr on June 04, 2021, 04:22:31 AM
Thanks StarGeek!

I just attached the json file.
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: Phil Harvey on June 04, 2021, 07:07:28 AM
I have modified the JSON file so it is compatible with the ExifTool -j option.  I don't have time to finish this right now, but further modification of the file and the -j=JSONFILE option should allow you to do what you want with a command like this (assuming all JPG's are in the current directory):

exiftool -j=posts_1.json -d %s .

Get this working on a few test files before running it on the lot.

- Phil
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: charlesr on June 04, 2021, 09:38:55 AM
Hey Phil, wow awesome! Modifying the json file: was it just a few find and replace actions? Or something more complicated?
I'll be doing this multiple times for many different json files and photos batches, so I'm trying to find a process that can be replicable.
I'd love to know how to modify the json file as needed :)
Thanks!
Title: Re: Sidecar JSON to JPG meta with keywords and file name change
Post by: Phil Harvey on June 04, 2021, 10:15:35 AM
The modification was a few search/replace operations, but I used a regex search pattern for some of these, so some where a bit complicated.  Basically, I removed all of the unnecessary wrappers ("media", "media_metadata", "photo_metadata" and "exif_data"), then changed the SourceFile paths and the latitude/longitude tag names.

If you do it wrong, you can easily generate a file that ExifTool won't read properly.  It took me a few tries to get something that worked.

- Phil