Home > Mac administration, Scripting > Using the plutil command line tool to work with JSON on macOS Monterey and later

Using the plutil command line tool to work with JSON on macOS Monterey and later

One of the issues Mac admins may face is working with JSON files as part of shell scripting. There are several solutions to this problem, including using the third-party jq command line tool and Apple’s JavaScript for Automation (JXA) interface. For posts on using these solutions, please see the links below:

jq:

JXA:

Another available option is to use the plutil command line tool on macOS Monterey and later to do the following:

  • Read values from JSON files
  • Convert plist files in XML format to JSON

For more details, please see below the jump.

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. For example, you may have a JSON file with the following keys and values:


{
"checkInFrequency": 0,
"createHooks": false,
"hookLog": false,
"hookPolicies": false,
"createStartupScript": false,
"startupLog": false,
"startupPolicies": false,
"startupSsh": false,
"enableLocalConfigurationProfiles": false
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

You could use the following command to extract the value for the createStartupScript key in the JSON file:


plutil -extract createStartupScript raw /path/to/filename.json

view raw

gistfile1.txt

hosted with ❤ by GitHub

In that case, you should see the following output:


username@computername ~ % plutil -extract createStartupScript raw filename.json
false
username@computername ~ %

view raw

gistfile1.txt

hosted with ❤ by GitHub

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. For example, you could run the following command to accomplish the following:

1. Create an XML file using the plutil tool

2. Add the following key and value, with the value stored in an array as a string:

  • Key: MyKeyHere
  • Value: MyGreatValue


plutil -create xml1 file.json
plutil -insert MyKeyHere -xml "<array><string>MyGreatValue</string></array>" file.json

view raw

gistfile1.txt

hosted with ❤ by GitHub

That would give you the following XML file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
<key>MyKeyHere</key>
<array>
<string>MyGreatValue</string>
</array>
</dict>
</plist>

view raw

gistfile1.txt

hosted with ❤ by GitHub

You would then run the following command to have plutil convert the XML in the file into the equivalent JSON:


plutil -convert json file.json

view raw

gistfile1.txt

hosted with ❤ by GitHub

That would give you the following JSON file:


{"MyKeyHere":["MyGreatValue"]}

view raw

gistfile1.txt

hosted with ❤ by GitHub

Something to be aware of is that there will be some limitations to this technique. plutil is designed to work with plist files, which means that if something isn’t formatted like it expects, plutil may not know how to handle it. Two limitations I know of are these:

  • NULL values in JSON files – There’s no equivalent for NULL in plist files, so the plutil tool will fail to convert JSON files with NULL values to a plist file in XML format.
  • Plists which contain date or data values – There’s no JSON equivalent of the date or data values in plist files, so plutil will fail to convert plist files in XML format with these values to JSON files.

Hat tip to Pico in the MacAdmins Slack for telling me about these limitations.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: