Exiftool generated json file: Exposure Time representation

Started by exifacou, November 06, 2024, 12:24:19 PM

Previous topic - Next topic

exifacou


Exiftool generated json file: Exposure Time representation

I'm writing a Swift program on my Mac that uses standard json decoding/encoding. The program reads a json file generated by Exiftool.

Example:

> exiftool -struct -j -ExposureTime -m -q -ext JPG . > images_ETselect.json

Result

[{
  "SourceFile": "./image-1.jpg",
  "ExposureTime": "1/125"
},
{
  "SourceFile": "./image-2.jpg",
  "ExposureTime": 1
},
{
  "SourceFile": "./image-3.jpg",
  "ExposureTime": 1.6
},
{
  "SourceFile": "./image-4.jpg",
  "ExposureTime": "1/13"
},
{
  "SourceFile": "./image-5.jpg",
  "ExposureTime": "1/640"
}]

Unfortunately the type of data generated for ExposureTime isn't always the same. For fractions it writes a string "1/8" which is exactly what I want.

But when Exiftool comes to an image with numbers, by example 1, 8, 1.6 ... it outputs a number not a string. Swift doesn't like this. My program crashes because of different data types for tag ExposureTime ...

debugDescription: "Expected to decode String but found number instead."

I read the exiftool manual but didn't find an example that describes what exiftool parameter I could set so that it always delivers a string for ExposureTime. Any idea how to do this?

Many thanks for your hints!

Phil Harvey

You could either add -n to the command to always output a real number, and/or -API StructFormat=JSONQ to always output the value as a string.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Phil Harvey

I'll add this to the documentation of the -j option:

    Note that ExifTool quotes JSON values only if they don't look like numbers
    (regardless of the original storage format or the relevant metadata
    specification).  This may be a problem when reading the JSON via a strongly
    typed language.  However, the API StructFormat option may be set to "JSONQ"
    to force quoting of numbers.  As well, the B<-sep> option may be used to
    convert arrays into strings.  For example:
   
        exiftool -j -api structformat=jsonq -sep ", " ...


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

exifacou

Quote from: Phil Harvey on November 06, 2024, 12:39:57 PMAs well, the B<-sep> option may be used to
    convert arrays into strings.

I had such a problem with "Keywords". In the case of only one keyword I got a string and not an array of strings which also caused problems. I solved it using XMP:Subject instead plus -struct option.

exifacou

Now tried this

exiftool -struct -j -ExposureTime -API structformat=jsonq -m -q -ext JPG . > images_ETselect.json

unfortunately besides the "String fractions" still numbers are delivered. Never worked with the API options. So I might do it wrong ...

StarGeek

Make sure you update to at least version 12.88, as that is where the JSONQ option was added.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

exifacou


exifacou