JSON Serializer - 2022/09/07 - beta.8

Post your working scripts, libraries and tools.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

JSON Serializer - 2022/09/07 - beta.8

18 Apr 2020, 08:19

I found this script on GitHub thanks to user @Coco (if I'm not mistaken).

For AHK v2:
  • all quoted strings are properly detected regardless of contents (like numbers)
  • all linear arrays and maps are properly detected even when empty
  • unquoted numbers are output as unquoted on dump, and are properly read on load
This isn't as fancy as @HotKeyIt's Yaml and Json parser but it gets the job done.

===================================================================
Updates
===================================================================

:arrow: Download on Github
Last edited by TheArkive on 07 Sep 2022, 12:44, edited 38 times in total.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - Jxon (thanks coco!) - AHK v1 / v2

19 Apr 2020, 06:37

=========================================================================================
AHK v1 JSON Serializer moved to this thread.
=========================================================================================
Last edited by TheArkive on 05 May 2021, 12:38, edited 9 times in total.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - Jxon - AHK v1/2 - 2020/06/20

20 Jun 2020, 15:04

Latest Update: 2020/07/09

Updated AHK v1 / v2 JXON for escaped /. It was commented out (can't remember why). User @Tigerlily caught this and reported it.
Last edited by TheArkive on 09 Jul 2020, 01:07, edited 1 time in total.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

08 Jul 2020, 19:16

@TheArkive
Thank you for posting this. I'm running into an issue with the first string i tried to parse, can you let me know what I'm doing wrong?

code:

Code: Select all

word := "wonderful"

/*
{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}
*/

json := FileRead( A_ScriptDir "\dictionary_" word ".txt" )

wordInfo := Jxon_Load( json )

MsgBox wordInfo["glossary", "title"]
error:
Image
-TL
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

08 Jul 2020, 20:42

Separate out your keys to both be in brackets. Or just use dot notation.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

08 Jul 2020, 23:52

kczx3 wrote:
08 Jul 2020, 20:42
Separate out your keys to both be in brackets. Or just use dot notation.
I tried dot notation and receive this error:

Image

However, for some reason bracket notation worked fine so I'll stick with that for now.. although dot notation would be a little less typing :think:
-TL
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

09 Jul 2020, 00:14

In AHK v1, keys vs dot notation is interchangable. In AHK v2 it isn't. So if you are working with a map you need obj["key"] notation.

Did ya get it working ok?
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

09 Jul 2020, 00:22

I'm receiving this string from the Merriam Webster dictionary:

Code: Select all

{"meta":{"id":"wonderful","uuid":"70456b1b-72ae-43ed-85cd-3e19ccf4b864","sort":"230207100","src":"collegiate","section":"alpha","stems":["wonderful","wonderfuller","wonderfullest","wonderfully","wonderfulness","wonderfulnesses"],"offensive":false},"hwi":{"hw":"won*der*ful","prs":[{"mw":"\u02c8w\u0259n-d\u0259r-f\u0259l","sound":{"audio":"wonder03","ref":"c","stat":"1"}}]},"fl":"adjective","def":[{"sseq":[[["sense",{"sn":"1","dt":[["text","{bc}exciting {a_link|wonder} {bc}{sx|marvelous||}, {sx|astonishing||} "],["vis",[{"t":"a sight {wi}wonderful{\/wi} to behold"}]]]}]],[["sense",{"sn":"2","dt":[["text","{bc}unusually good {bc}{sx|admirable||} "],["vis",[{"t":"did a {wi}wonderful{\/wi} job"}]]]}]]]}],"uros":[{"ure":"won*der*ful*ly","prs":[{"mw":"\u02c8w\u0259n-d\u0259r-f(\u0259-)l\u0113","sound":{"audio":"wonder04","ref":"c","stat":"1"}}],"fl":"adverb"},{"ure":"won*der*ful*ness","prs":[{"mw":"\u02c8w\u0259n-d\u0259r-f\u0259l-n\u0259s","sound":{"audio":"wonder05","ref":"c","stat":"1"}}],"fl":"noun"}],"date":"before 12th century{ds||1||}","shortdef":["exciting wonder : marvelous, astonishing","unusually good : admirable"]}
beautified:

Code: Select all

{
	"meta": {
		"id": "wonderful",
		"uuid": "70456b1b-72ae-43ed-85cd-3e19ccf4b864",
		"sort": "230207100",
		"src": "collegiate",
		"section": "alpha",
		"stems": ["wonderful", "wonderfuller", "wonderfullest", "wonderfully", "wonderfulness", "wonderfulnesses"],
		"offensive": false
	},
	"hwi": {
		"hw": "won*der*ful",
		"prs": [{
			"mw": "\u02c8w\u0259n-d\u0259r-f\u0259l",
			"sound": {
				"audio": "wonder03",
				"ref": "c",
				"stat": "1"
			}
		}]
	},
	"fl": "adjective",
	"def": [{
		"sseq": [
			[
				["sense", {
					"sn": "1",
					"dt": [
						["text", "{bc}exciting {a_link|wonder} {bc}{sx|marvelous||}, {sx|astonishing||} "],
						["vis", [{
							"t": "a sight {wi}wonderful{\/wi} to behold"
						}]]
					]
				}]
			],
			[
				["sense", {
					"sn": "2",
					"dt": [
						["text", "{bc}unusually good {bc}{sx|admirable||} "],
						["vis", [{
							"t": "did a {wi}wonderful{\/wi} job"
						}]]
					]
				}]
			]
		]
	}],
	"uros": [{
		"ure": "won*der*ful*ly",
		"prs": [{
			"mw": "\u02c8w\u0259n-d\u0259r-f(\u0259-)l\u0113",
			"sound": {
				"audio": "wonder04",
				"ref": "c",
				"stat": "1"
			}
		}],
		"fl": "adverb"
	}, {
		"ure": "won*der*ful*ness",
		"prs": [{
			"mw": "\u02c8w\u0259n-d\u0259r-f\u0259l-n\u0259s",
			"sound": {
				"audio": "wonder05",
				"ref": "c",
				"stat": "1"
			}
		}],
		"fl": "noun"
	}],
	"date": "before 12th century{ds||1||}",
	"shortdef": ["exciting wonder : marvelous, astonishing", "unusually good : admirable"]
}

I am receiving an error using this code:

Code: Select all

word := "wonderful"
Download( "https://www.dictionaryapi.com/api/v3/references/collegiate/json/" word "?key=YOUR-API-KEY", A_ScriptDir "\dictionary_" word ".txt" )
json := FileRead( A_ScriptDir "\dictionary_" word ".txt" )
json := Trim( json, "[]" ) ; for some reason the returned string comes with a set of square brackets encasing the JSON object
wordInfo := Jxon_Load( json )
MsgBox wordInfo["meta"]["id"]
error:
Image

the culprit:

Code: Select all

							"t": "a sight {wi}wonderful{\/wi} to behold"
						}]]
					]
				}]
			],
			[
				["sense", {
					"sn": "2",
					"dt": [
						["text", "{bc}unusually good {bc}{sx|admirable||} "],
						["vis", [{
							"t": "did a {wi}wonderful{\/wi} job"
when I remove the two \/ everything seems to work fine. I checked and the string is valid JSON, is this the APIs string causing this issue?
Last edited by Tigerlily on 09 Jul 2020, 00:24, edited 1 time in total.
-TL
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

09 Jul 2020, 00:23

@TheArkive
thanks for the heads up, that makes sense why the dot notation isn't working.

i got it working with a very simple json example string, however, now after trying to pull string from an API, getting the above issue.
-TL
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

09 Jul 2020, 00:37

Try NOT trimming the brackets.

the Brackets represent a linear array []. Curly braces {} represent a map (or associative array). The top level of the json element will have one of these always. That's how objects are saved / layered. By removing this, you are actually breaking it.

Let me know what happens when you don't trim the braces.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a113

09 Jul 2020, 00:44

Oh i see ... top level is [] ... and it still has {} as the next level down? that's a little excessive ;P ... ok i'm trying to load this up and see what i see

EDIT: i got it to load. Gotta re-add the escape for /. Should be around line 71 ish:

val := StrReplace(val, "\/", "/")

uncomment this line and try again.

EDIT: It's working... return value was "wonderful" as expected.

EDIT: Just to be extra sure more errors wouldn't pop up, it's double confirmed it was the escape for / ... so where you see \/ was causing it. I had that line that did that replacement commented out, not sure why. But thanks for reporting! I'll fix this in the OP.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/01 - a115

09 Jul 2020, 20:41

@TheArkive
Thanks, you rock!

Really appreciate you updating this lib for a115!!!!! :bravo:
-TL
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: YEJP - Yet Another Json Parser - AHK v1/2 - 2020/07/31 - a121

10 Oct 2020, 13:52

Update 2020/10/10 - for AHK v1 version (post #2)
  • Fixed an error when trying to load arrays. Updated Jxon_dump(), now it properly quotes keys that are numerical values in Object() arrays. Unquoted keys were the cause of the error.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

31 Oct 2020, 21:17

Hey, man, I just wanted to thank you for updating this. Good stuff, and much needed. :bravo:
User avatar
Curdflappers
Posts: 11
Joined: 13 Nov 2020, 03:18
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

14 Nov 2020, 03:19

@TheArkive Thanks so much! I've been working on converting this for a few days, but I'm very new to AHK syntax. I'm working on this script on GitHub, do you mind if I incorporate your changes to my repo, crediting you, of course?
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

14 Nov 2020, 05:05

@Curdflappers
Sure man, no problem. What exactly are you trying to convert? I have both AHK v1 and v2 scripts posted in this thread. AHK v1 version is in post #2.
User avatar
Curdflappers
Posts: 11
Joined: 13 Nov 2020, 03:18
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

14 Nov 2020, 05:23

@TheArkive: I'm working on updating Chrome.ahk by G33kDude, which allows AHK scripts to navigate Chromium browsers like Google Chrome and Microsoft Edge. It logs through an old JSON-parsing library originally written by cocobelgica. In order to update Chrome.ahk, I need to update the JSON-parsing library. :)
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

14 Nov 2020, 17:17

@Curdflappers
Look at the 2nd post in this thread. My library is actually already converted from cocobelgica (see 1st post, 1st link). Just don't want you to spend unnecessary time if ya don't have to.

Post #1 is for AHK v2
Post #2 is for AHK v1

Hopefully you can just copy-paste for whatever version of AHK you are writing / converting Chrome.ahk for.
Last edited by TheArkive on 15 Nov 2020, 05:26, edited 1 time in total.
User avatar
Curdflappers
Posts: 11
Joined: 13 Nov 2020, 03:18
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

15 Nov 2020, 01:15

Yeah, you're right, I didn't think my response through. I'm working on updating a fork of Coco (and G33kDude)'s repository, so I wanted to verify that this was OK to publish on my account. I'll give you credit in the script, of course!

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: AHK_user and 9 guests