JSON Tools

michaelmcgaw
New Contributor III

Is there a macOS command-line tool available for JSON that provides parsing functions like xmllint?

I seem to be able to find lots of validators and pretty-printers. But I need a tool I can run inside shell scripts to parse output responses from Jamf Pro's API.

6 REPLIES 6

stevewood
Honored Contributor II
Honored Contributor II

 jq is what comes to mind first: https://stedolan.github.io/jq/

But it is not installed by default, so you would have to install it on a system before being able to query JSON.

@mm2270 posted a way to parse JSON in shell, but I do not recall which post it was in and I'm too lazy to go find it (sorry Mike :-) )

+1 for jq. It's a must have. plutil can extract data from json but you need to have Monterey installed

mm2270
Legendary Contributor III

Hmm, I wish I could remember what that was also. 🤔

I do think at one time I found a way to "pretty print" JSON input using python, but that certainly isn't the same as parsing the JSON. And anything relying on legacy python 2 at this point I've removed or am removing from any of my scripts.

There's very likely a python 3 method to do the same thing. But then you need to ship python 3 to your Macs. 😕 It might just be easier to deploy jq, which comes up in almost any question out on the web on how to parse JSON, though I've never used it myself.

gavin_pardoe
New Contributor III

You can also use the JavaScript objective-c bridge in Apple script via ZSH without needing to install anything else.

 

add the below to a bash/zsh script

deviceJsonData=$(Json data source)

userJsonData=$(Json data source)

getJson() {
# $1: JSON string to parse, $2: JSON key to look up
JSON="$1" osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env).$2"
}

parse by passing with dot notation:

device=$(getJson "$deviceJsonData" 'device')
firstName=$(getJson "$userJsonData" 'profile.firstName')

Recommend this. It works in bash as well. I have been using it to run scripts utilizing the API on users' devices without having to push out tools. Very useful.

brockwalters
Contributor II

The binary you were thinking of is json_pp - 

 

 

% echo '{"SPHardwareDataType":[{"_name":"hardware_overview","activation_lock_status":"activation_lock_disabled","boot_rom_version":"10151.61.4","chip_type":"Apple M2 Pro","machine_model":"Mac14,10","machine_name":"MacBook Pro","model_number":"MNW83LL/A","number_processors":"proc 12:8:4","os_loader_version":"10151.61.4","physical_memory":"16 GB","platform_UUID":"D07A2309-7CD0-5086-ACF4-A99DA8688860","provisioning_UDID":"00006020-001A01E222C3C01E","serial_number":"MQVWG6NM2J"}]}' | json_pp -t json
{
   "SPHardwareDataType" : [
      {
         "_name" : "hardware_overview",
         "activation_lock_status" : "activation_lock_disabled",
         "boot_rom_version" : "10151.61.4",
         "chip_type" : "Apple M2 Pro",
         "machine_model" : "Mac14,10",
         "machine_name" : "MacBook Pro",
         "model_number" : "MNW83LL/A",
         "number_processors" : "proc 12:8:4",
         "os_loader_version" : "10151.61.4",
         "physical_memory" : "16 GB",
         "platform_UUID" : "D07A2309-7CD0-5086-ACF4-A99DA8688860",
         "provisioning_UDID" : "00006020-001A01E222C3C01E",
         "serial_number" : "MQVWG6NM2J"
      }
   ]
}

 

 

Please also see: https://community.jamf.com/t5/jamf-pro/json-amp-the-arg-nauts/td-p/307625