Jump to content

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

Need help reading json file into array variables


  • Please log in to reply
7 replies to this topic
jasdjensen
  • Members
  • 6 posts
  • Last active: Jul 16 2011 12:20 AM
  • Joined: 15 Jun 2010
Given the following json file format:

{"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}


I'm trying, with poor results so far, to read in a file c:\summary.json into a variable array so that it produces:

summary.variable1=var1data
summary.variable2=var2data

etc.

I know this must be simple, and I've read a lot of complex posts, but I still don't get it. Can someone dumb this down for me?


I can get this to work:
summary := {"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}

but I cant get it to work by reading from a file.


Any help is appreciated. Thanks!

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
JSON Parser
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

jasdjensen
  • Members
  • 6 posts
  • Last active: Jul 16 2011 12:20 AM
  • Joined: 15 Jun 2010
Parsing isn't the problem. If I specify the json strings in the code I can parse them without issue.

The problem is reading them from the file into an array. Or maybe I dont understand what you're trying to tell me.

I can even get the json string into a variable with:

FileRead, summaryraw, c:\solar\summary.json
summary := summaryraw


I just cant get it to store it as an array. It just stores it as a string in %summaryraw%

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

The problem is reading them from the file into an array.

Start by reading from the file into a string. Then ...

Parsing isn't the problem.

Based on what you stated, I can only assume you want one of the following:
json = {variable1:"var1data",variable2:"var2data"}

obj := {}
Loop, Parse, json, `,, {}
{
   sep := InStr(A_LoopField, ":")
   obj[SubStr(A_LoopField,1,sep-1)] := Trim(SubStr(A_LoopField,sep+1), """")
}
MsgBox, % obj.variable1 "`n" obj.variable2
var1data := "foo"
var2data := "bar"
json = {variable1:var1data,variable2:var2data}

obj := {}
Loop, Parse, json, `,, {}
{
   sep := InStr(A_LoopField, ":")
   VarName := SubStr(A_LoopField,sep+1)
   obj[SubStr(A_LoopField,1,sep-1)] := %VarName%
}
MsgBox, % obj.variable1 "`n" obj.variable2


jasdjensen
  • Members
  • 6 posts
  • Last active: Jul 16 2011 12:20 AM
  • Joined: 15 Jun 2010
I'm sorry, I'm sure it's obvious that I'm a novice.

if I do this in a script:
summary := {"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}

Then I get the results as follows:
summary.variable1=var1data
summary.variable2=var2data
..etc

But I dont want to specify what the data is, I want to read it from the summary.json file and have it show up in the variable array.

I was thinking I could do something like this to get it from the file, put it into a variable as a string, then treat the string as a variable array:

FileRead, summaryraw, c:\solar\summary.json
summary := summaryraw


but that doesn't seem to work like I thought it would.

  • Guests
  • Last active:
  • Joined: --
Doesn't work that way :-(
But perhaps this will help json <-> objects <!-- m -->http://www.autohotke...pic.php?t=66070<!-- m -->

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Put jethrow's code in a function.

FileRead, summaryraw, c:\solar\summary.json

summary := json_parse(summaryraw)
Msgbox % summary.variable4


json_parse(json) {
	obj := {}
	Loop, Parse, json, `,, {}
	{
	   sep := InStr(A_LoopField, ":")
	   obj[Trim(SubStr(A_LoopField,1,sep-1), """")] := Trim(SubStr(A_LoopField,sep+1), """")
	}
	return obj
}

aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

jasdjensen
  • Members
  • 6 posts
  • Last active: Jul 16 2011 12:20 AM
  • Joined: 15 Jun 2010

Put jethrow's code in a function.

FileRead, summaryraw, c:\solar\summary.json

summary := json_parse(summaryraw)
Msgbox % summary.variable4


json_parse(json) {
	obj := {}
	Loop, Parse, json, `,, {}
	{
	   sep := InStr(A_LoopField, ":")
	   obj[Trim(SubStr(A_LoopField,1,sep-1), """")] := Trim(SubStr(A_LoopField,sep+1), """")
	}
	return obj
}





Thanks for making that easy for me. Now it's working. It pulls from the file, inserts the data into the array and looks correct.


Thanks for everyone's help. Sorry I was slow to get it.