Tuesday, June 9, 2020

plutil Enhanced in Catalina

Dominik Wagner:

TIL: plutil in catalina also supports objc and swift as output format. So if you want to quickly convert a e.g. json to inline code format, plutil might be the thing for you.

Previously, plutil was mostly useful for validating propery lists and for converting between XML, binary, and JSON representations. Now it can also generate Objective-C and Swift literals. And there are new commands for inserting, replacing, removing, and extracting a value at a particular key path. This seems less powerful than PlistBuddy but with a more standard interface.

Previously:

Update (2021-01-12): Unfortunately, plutil will crash if you try to set the value of a dictionary key whose first character is a number.

Update (2023-04-27): Rich Trouton:

If you want to read JSON values from a file, you can use the raw option of plutil’s -extract function in some cases to extract values from keys in JSON files.

[…]

In cases like this, where you’re dealing with a JSON file with a fairly simple format (without arrays or otherwise nested values), plutil is a good tool which is built into macOS that you can call on to extract the data you need.

Another option is using the plutil tool to write what you need to an XML file, then use plutil’s -convert functionality to turn it into a JSON file. For folks more experienced with using plutil to write XML to a file than they are with writing JSON, this option may help with a lot of use cases.

Previously:

1 Comment RSS · Twitter

The key path manipulation arguments have been supported for a long time. The man page wasn’t updated until recently.

Leave a Comment