Jump to content

Parsing Rest API Json or XML to something readable like an array


 Share

Recommended Posts

Hey all,

 

I have been fussing around with Rest API from Airwatch MDM trying to retrieve some information about my devices (corp environment).  I gave up doing it natively in autoit for now and decided on running Curl.exe from a windows command prompt,

Here is an example of that command

curl "!!!URL!!/api/mam/apps/purchased/search?applicationname=Angry%20Birds%20Go!&locationgroupid=1098" \  -H "Accept: application/json" \  -H "Authorization: Basic !!APIKey!!!=" \  -H "aw-tenant-code: !!!!!"

 

Here is an example of those outputs.  I have looked at the JSON and XML UDFs but I don't really understand how they function.  Any help on this would be helpful

Json

{"Application":[{"ApplicationName":"Angry Birds Go!","ApplicationSize":"436.56 MB","ApplicationUrl":"https://itunes.apple.com/us/app/angry-birds-go!/id642821482?mt=8&uo=4","Assignments":[{"Status":"Active","SmartGroupId":4193,"LocationGroupId":"0","Users":0,"Allocated":1,"Redeemed":0,"AssignmentRuleType":"License"}],"BundleId":"com.rovio.angrybirdsgo","IsReimbursable":false,"LargeIconUri":"","MediumIconUri":"","SmallIconUri":"","LocationGroupId":1098,"Platform":2,"ManagedDistribution":{"Purchased":1,"Burned":0,"OnHold":0,"Available":1,"AppLicenseEligibility":"Device"},"Deployment":{"AssignmentType":"OnDemand","RemoveOnUnenroll":true,"PreventApplicationBackup":true,"UseVPN":false},"AppType":"Purchased","AssignmentStatus":"Assigned","IsAutoUpdateEnabled":false,"Id":{"Value":140}}],"Page":0,"PageSize":500,"Total":1}

 

XML

<?xml version="1.0" encoding="utf-8"?><PurchasedApplicationSearchResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.air-watch.com/servicemodel/resources"><Page>0</Page><PageSize>500</PageSize><Total>1</Total><Application><PurchasedApplicationEntity><Id xmlns="">140</Id><ApplicationName>Angry Birds Go!</ApplicationName><ApplicationSize>436.56 MB</ApplicationSize><ApplicationUrl>https://itunes.apple.com/us/app/angry-birds-go!/id642821482?mt=8&amp;uo=4</ApplicationUrl><Assignments><Assignment><Status>Active</Status><SmartGroupId>4193</SmartGroupId><LocationGroupId>0</LocationGroupId><Users>0</Users><Allocated>1</Allocated><Redeemed>0</Redeemed><AssignmentRuleType>License</AssignmentRuleType></Assignment></Assignments><BundleId>com.rovio.angrybirdsgo</BundleId><IsReimbursable>false</IsReimbursable><LargeIconUri /><MediumIconUri /><SmallIconUri /><LocationGroupId>1098</LocationGroupId><Platform>2</Platform><ManagedDistribution><Purchased>1</Purchased><Burned>0</Burned><OnHold>0</OnHold><Available>1</Available><AppLicenseEligibility>Device</AppLicenseEligibility></ManagedDistribution><Deployment><AssignmentType>OnDemand</AssignmentType><RemoveOnUnenroll>true</RemoveOnUnenroll><PreventApplicationBackup>true</PreventApplicationBackup><UseVPN>false</UseVPN><VPNProfileId xsi:nil="true" /></Deployment><AppType>Purchased</AppType><AssignmentStatus>Assigned</AssignmentStatus><IsAutoUpdateEnabled>false</IsAutoUpdateEnabled></PurchasedApplicationEntity></Application></PurchasedApplicationSearchResult>

Link to comment
Share on other sites

1 hour ago, wisem2540 said:

I gave up doing it natively in autoit for now and decided on running Curl.exe from a windows command prompt

 

1 hour ago, wisem2540 said:

I have looked at the JSON and XML UDFs but I don't really understand how they function.  Any help on this would be helpful

I doubt that your request for assistance could have been much more vague.  What is it that you want help with?  Do you want help retrieving the JSON using AutoIt natively instead of using a CURL command?  Do you want help parsing the data that you are already retrieving using the CURL command?  If it is help parsing the JSON data, what values from the result are you wanting to parse?  Or is it that you need help understanding how to use the json or xml UDFs?  The more specific your request is, the more likely you will be to get someone interested enough to help you.  Just throwing some vague request out there, without specifying any details on what you need help with, will most likely just get ignored -- which is probably what I should have done.  :think::) 

What have you tried in your efforts to accomplish your goal.  What exactly are you having a problem understanding? Do you prefer parsing XML or JSON? 

Edited by TheXman
Link to comment
Share on other sites

17 hours ago, TheXman said:

 

I doubt that your request for assistance could have been much more vague.  What is it that you want help with?  Do you want help retrieving the JSON using AutoIt natively instead of using a CURL command?  Do you want help parsing the data that you are already retrieving using the CURL command?  If it is help parsing the JSON data, what values from the result are you wanting to parse?  Or is it that you need help understanding how to use the json or xml UDFs?  The more specific your request is, the more likely you will be to get someone interested enough to help you.  Just throwing some vague request out there, without specifying any details on what you need help with, will most likely just get ignored -- which is probably what I should have done.  :think::) 

What have you tried in your efforts to accomplish your goal.  What exactly are you having a problem understanding? Do you prefer parsing XML or JSON? 

All I really need are they Properties and values into a 2D Array. 

Link to comment
Share on other sites

@wisem2540 Here's a quick and dirty example with your JSON string --

#include <JSON.au3>

$sJson = '{"Application":[{"ApplicationName":"Angry Birds Go!","ApplicationSize":"436.56 MB","ApplicationUrl":"https://itunes.apple.com/us/app/angry-birds-go!/id642821482?mt=8&uo=4","Assignments":[{"Status":"Active","SmartGroupId":4193,"LocationGroupId":"0","Users":0,"Allocated":1,"Redeemed":0,"AssignmentRuleType":"License"}],"BundleId":"com.rovio.angrybirdsgo","IsReimbursable":false,"LargeIconUri":"","MediumIconUri":"","SmallIconUri":"","LocationGroupId":1098,"Platform":2,"ManagedDistribution":{"Purchased":1,"Burned":0,"OnHold":0,"Available":1,"AppLicenseEligibility":"Device"},"Deployment":{"AssignmentType":"OnDemand","RemoveOnUnenroll":true,"PreventApplicationBackup":true,"UseVPN":false},"AppType":"Purchased","AssignmentStatus":"Assigned","IsAutoUpdateEnabled":false,"Id":{"Value":140}}],"Page":0,"PageSize":500,"Total":1}'

Json_Dump($sJson)
$oJson = Json_Decode($sJson)
ConsoleWrite(Json_Get($oJson,".Application[0].ApplicationName") & @CRLF)

$oApp=Json_Get($oJson,".Application[0]")
Json_Dump(Json_Encode($oApp))

I don't know of a way to dump it directly to an array, but you can certainly code it yourself. :thumbsup:

Link to comment
Share on other sites

For the fun of it :

#include <Array.au3>

Opt ("MustDeclareVars", 1)

Local $sJson = '{"Application":[{"ApplicationName":"Angry Birds Go!","ApplicationSize":"436.56 MB","ApplicationUrl":"https://itunes.apple.com/us/app/angry-birds-go!/id642821482?mt=8&uo=4","Assignments":[{"Status":"Active","SmartGroupId":4193,"LocationGroupId":"0","Users":0,"Allocated":1,"Redeemed":0,"AssignmentRuleType":"License"}],"BundleId":"com.rovio.angrybirdsgo","IsReimbursable":false,"LargeIconUri":"","MediumIconUri":"","SmallIconUri":"","LocationGroupId":1098,"Platform":2,"ManagedDistribution":{"Purchased":1,"Burned":0,"OnHold":0,"Available":1,"AppLicenseEligibility":"Device"},"Deployment":{"AssignmentType":"OnDemand","RemoveOnUnenroll":true,"PreventApplicationBackup":true,"UseVPN":false},"AppType":"Purchased","AssignmentStatus":"Assigned","IsAutoUpdateEnabled":false,"Id":{"Value":140}}],"Page":0,"PageSize":500,"Total":1}'
ClipPut ($sJson)
$sJson = StringRegExpReplace ($sJson, '(https?)(:)\/\/', "$1;\/\/")
Local $aArray = StringSplit ($sJson, ":,{", $STR_CHRSPLIT)
;_ArrayDisplay ($aArray)
Local $aFinal[Round($aArray[0]/2)][2], $iStart = 0, $iFinal = 0
Local $sP1, $sP2
While True
  $iStart += 2
  If $iStart > $aArray[0] Then ExitLoop
  $sP1 = $aArray[$iStart]
  $sP2 = StringRegExpReplace ($aArray[$iStart+1],"}*]*$","")
  If $sP2 = "" or $sP2 = "[" Then ContinueLoop
  $aFinal[$iFinal][0] = $sP1
  $aFinal[$iFinal][1] = $sP2
  $iFinal += 1
WEnd
redim $aFinal[$iFinal][2]
_ArrayDisplay ($aFinal)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...