Using the plutil command line tool to work with JSON on macOS Monterey and later
2023-4-16 03:50:40 Author: derflounder.wordpress.com(查看原文) 阅读量:19 收藏

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
}

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


In that case, you should see the following output:


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


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>

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


That would give you the following JSON file:


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.


文章来源: https://derflounder.wordpress.com/2023/04/15/using-the-plutil-command-line-tool-to-work-with-json-on-macos-monterey-and-later/
如有侵权请联系:admin#unsafe.sh