Quantcast
Channel: rtrouton – Der Flounder
Viewing all articles
Browse latest Browse all 764

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

$
0
0

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:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


{
"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:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


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:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


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



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


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:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


<?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:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


plutil -convert json file.json
view raw

gistfile1.txt

hosted with ❤ by GitHub

That would give you the following JSON file:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


{"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.


Viewing all articles
Browse latest Browse all 764

Trending Articles