Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

JSON Parsing Solution?



  • Please log in to reply
6 replies to this topic
stemsmit
  • Members
  • 17 posts
  • Last active: Feb 06 2015 11:17 PM
  • Joined: 16 May 2013

I'm trying to parse a json file with query results retrieved through the ZenDesk API into a multidimensional array using AHK_L 1.1.10.01 and some other versions. I've attempted everything on these forums but have had no luck what so ever.

 

I've tried this: http://www.autohotke...-l-json-object/ and the other examples on it including the AHK v2 scripts and ScriptControl

And also attempted: http://www.autohotke...array-variables , http://www.autohotke...ode-und-decode/

 

 

Is there a JSON parsing solution that adds all the keys (email, namepassword etc. ) and multiple variables ([email protected],[email protected],name1,name2, etc) in each as an object or multidimensional array? I already have the file with the JSON information( not structured ) automatically downloaded from the API using cURL command line tool.

 

Below is an example of the JSON I'm trying to parse:

{"users":[{"url":"https:wwww.website.com/file.json","id":55554,"external_id":null,"name":"some name1","alias":null,"created_at":"2012-03-22T17:12:46Z","updated_at":"2012-03-22T17:12:46Z","active":true,"verified":false,"shared":false,"shared_agent":false,"locale_id":null,"locale":"en-US","time_zone":"Central Time (US & Canada)","last_login_at":null,"email":"[email protected]","phone":null,"signature":null,"details":null,"notes":null,"organization_id":null,"role":"end-user","custom_role_id":null,"moderator":false,"ticket_restriction":"requested","only_private_comments":false,"tags":[],"suspended":false,"photo":null},{"url":"https:wwww.website.com/file2.json","id":55555,"external_id":null,"name":"some name2",created_at":"2012-03-22T17:20:01Z","updated_at":"2012-03-22T17:20:01Z","active":true,"verified":false,"shared":false,"shared_agent":false,"locale_id":null,"locale":"en-US","time_zone":"Central Time (US & Canada)","last_login_at":null,"email":"[email protected]","phone":null,"signature":null,"details":null,"notes":null,"organization_id":null,"role":"end-user","custom_role_id":null,"moderator":false,"ticket_restriction":"requested","only_private_comments":false,"tags":[],"suspended":false,"photo":null}]


Edited by stemsmit, 21 May 2013 - 09:16 PM.


strobo
  • Members
  • 359 posts
  • Last active: Mar 10 2015 08:13 PM
  • Joined: 19 Jun 2012

You can try this one

http://www.autohotke...ode-und-decode/


Regards,
Babba

stemsmit
  • Members
  • 17 posts
  • Last active: Feb 06 2015 11:17 PM
  • Joined: 16 May 2013

I attempted that one as well. Again with no luck. Updated my original post.



Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

Here's another one: http://www.autohotke...in/#entry588268



stemsmit
  • Members
  • 17 posts
  • Last active: Feb 06 2015 11:17 PM
  • Joined: 16 May 2013

 

I put this code in and the I get an error(unrecognized action) in this area:

jsCode = (
    function arrangeForAhkTraversing(obj){


Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012
✓  Best Answer

There's a missing quotation mark in your JSON (") and it's missing an ending "}"

Try this:

src =
(
{
	"users":
	[
		{
			"url":"https:wwww.website.com/file.json",
			"id":55554,
			"external_id":null,
			"name":"some name1",
			"alias":null,
			"created_at":"2012-03-22T17:12:46Z",
			"updated_at":"2012-03-22T17:12:46Z",
			"active":true,
			"verified":false,
			"shared":false,
			"shared_agent":false,
			"locale_id":null,
			"locale":"en-US",
			"time_zone":"Central Time (US & Canada)",
			"last_login_at":null,
			"email":"[email protected]",
			"phone":null,"signature":null,
			"details":null,
			"notes":null,
			"organization_id":null,
			"role":"end-user",
			"custom_role_id":null,
			"moderator":false,
			"ticket_restriction":"requested",
			"only_private_comments":false,
			"tags":[],"suspended":false,
			"photo":null
		},
		{
			"url":"https:wwww.website.com/file2.json",
			"id":55555,"external_id":null,
			"name":"some name2",
			"created_at":"2012-03-22T17:20:01Z",
			"updated_at":"2012-03-22T17:20:01Z",
			"active":true,
			"verified":false,
			"shared":false,
			"shared_agent":false,
			"locale_id":null,
			"locale":"en-US",
			"time_zone":"Central Time (US & Canada)",
			"last_login_at":null,
			"email":"[email protected]",
			"phone":null,"signature":null,
			"details":null,
			"notes":null,
			"organization_id":null,
			"role":"end-user",
			"custom_role_id":null,
			"moderator":false,
			"ticket_restriction":"requested",
			"only_private_comments":false,
			"tags":[],
			"suspended":false,
			"photo":null
		}
	]
}
)


jsObj := JSON_parse(src)
MsgBox, % jsObj.users.1.url
return

/*
derived from Getfree's parseJson function
http://www.autohotkey.com/board/topic/93300-what-format-to-store-settings-in/#entry588268
credits to Getfree
*/
JSON_parse(jsonStr) {
	SC := ComObjCreate("ScriptControl") 
	SC.Language := "JScript"
	;ComObjError(false)
	jsCode =
	(
	function arrangeForAhkTraversing(obj) {
		if(obj instanceof Array) {
			for(var i=0 ; i<obj.length ; ++i)
				obj[i] = arrangeForAhkTraversing(obj[i]) ;
			return ['array',obj] ;
		} else if(obj instanceof Object) {
			var keys = [], values = [] ;
			for(var key in obj) {
				keys.push(key) ;
				values.push(arrangeForAhkTraversing(obj[key])) ;
			}
			return ['object',[keys,values]] ;
		} else
			return [typeof obj,obj] ;
	}
	)
	SC.ExecuteStatement(jsCode "; obj=" jsonStr)
	return convertJScriptObj2AHK(SC.Eval("arrangeForAhkTraversing(obj)"))
}

convertJScriptObj2AHK(jsObj) {
	if (jsObj[0]="object") {
		obj := {}, keys := jsObj[1][0], values := jsObj[1][1]
		loop % keys.length
			obj[keys[A_Index-1]] := convertJScriptObj2AHK(values[A_Index-1])
		return obj
	} else if (jsObj[0]="array") {
		array := []
		loop % jsObj[1].length
			array.Insert(convertJScriptObj2AHK(jsObj[1][A_Index-1]))
		return array
	} else return jsObj[1]
}


stemsmit
  • Members
  • 17 posts
  • Last active: Feb 06 2015 11:17 PM
  • Joined: 16 May 2013

Alright! That did it! This in combination with the AHK_L ANSI works like a charm. Thanks for the assistance Coco.