Skip to main content
Question

JSON Tools


Forum|alt.badge.img+5

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

stevewood
Forum|alt.badge.img+35
  • Employee
  • 1797 replies
  • May 8, 2022

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


Forum|alt.badge.img+8
stevewood wrote:

 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


Forum|alt.badge.img+5
  • New Contributor
  • 14 replies
  • May 9, 2022

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')


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • May 9, 2022
stevewood wrote:

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


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.


Fluffy
Forum|alt.badge.img+10
  • Honored Contributor
  • 209 replies
  • May 11, 2022
gavin_pardoe wrote:

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
Forum|alt.badge.img+8
  • Valued Contributor
  • 64 replies
  • January 9, 2024

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings