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!
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
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
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.
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 ...
Make sure you update to at least version 12.88 (https://exiftool.org/history.html#v12.88), as that is where the JSONQ option was added.
Sorry, I'll try this tomorrow. Many thanks!
Updated to 13.02 - Works - Great, many thanks!