Skip to content

Instantly share code, notes, and snippets.

@harkabeeparolus
Created July 19, 2023 09:35
Show Gist options
  • Save harkabeeparolus/5122d6bd27392620c952bab0b4f349ff to your computer and use it in GitHub Desktop.
Save harkabeeparolus/5122d6bd27392620c952bab0b4f349ff to your computer and use it in GitHub Desktop.
When `plutil --convert json` fails due to "invalid object in plist for destination format", use Python to serialize everything except for the binary data. Based on a Stack Overflow answer.
#! /usr/bin/env python3
"""Read Apple Plist files and output JSON data."""
# https://stackoverflow.com/a/72958435/5201675
import argparse as ap
import json
import plistlib
import sys
from typing import IO, Any
def main():
"""Run the main script logic."""
parser = ap.ArgumentParser(description=__doc__)
parser.add_argument("files", metavar="FILE", nargs="+", type=ap.FileType("rb"))
args = parser.parse_args()
for file in args.files:
print(plist2json(file))
def plist2json(file: IO[bytes]) -> str:
"""Parse a plist string and return a JSON string."""
def not_jsonable(_object: Any) -> str:
return "<not serializable>"
return json.dumps(plistlib.load(file), default=not_jsonable)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment