• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Dynamic search for json properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic search for json properties


  • Subject: Re: Dynamic search for json properties
  • From: Jörgen Stahle <email@hidden>
  • Date: Tue, 15 Nov 2016 21:24:11 +0000
  • Thread-topic: Dynamic search for json properties

I was wrong! The _javascript_ code DOES work with JSA!

var obj = JSON.parse('{\"timeblocksDefs\": { \"normal\": { \"morning\": \"06-09\", \"day\": \"09-18\", \"primetime\": \"18-22\", \"nighttime\": \"22-06\" }, \"lateprime\": { \"morning\": \"06-09\", \"day\": \"09-19\", \"primetime\": \"19-23\", \"nighttime\": \"23-06\" } }}');
JSON.stringify(obj.timeblocksDefs.lateprime);
—> "{\"morning\":\"06-09\",\"day\":\"09-19\",\"primetime\":\"19-23\",\"nighttime\":\"23-06\"}"

Then I just have to call JSA from Applescript. I Guess it is as easy as calling another applescript. (Thanks for the lead, Simon!)

Any thoughts about that, anyone? 








15 nov. 2016 kl. 22:14 skrev Jörgen Stahle <email@hidden>:

Thanks.

I’ve had _javascript_ in mind, but then in the form of Extendscript, since this script is going to run with Indesign server. But the JSON-methods you used here in your Safari example doesn’t work with Extendscript. (And calling Safari is of course not good solution for this use.)

I also tried your _javascript_ code with JSA, but JSON.stringify() doesn’t seem to work there either. Otherwise calling JSA would have been a nice solution.

J




15 nov. 2016 kl. 18:53 skrev Simon Forster <email@hidden>:


Cheating but…

Fire up Safari. In the “Develop” menu “Allow _javascript_ from Apple Events” then:

tell application "Safari"
set theString to do _javascript_ "var obj = JSON.parse('{\"timeblocksDefs\": { \"normal\": { \"morning\": \"06-09\", \"day\": \"09-18\", \"primetime\": \"18-22\", \"nighttime\": \"22-06\" }, \"lateprime\": { \"morning\": \"06-09\", \"day\": \"09-19\", \"primetime\": \"19-23\", \"nighttime\": \"23-06\" } }}');
JSON.stringify(obj.timeblocksDefs.lateprime);" in document 1
end tell
theString

—> "{\"morning\":\"06-09\",\"day\":\"09-19\",\"primetime\":\"19-23\",\"nighttime\":\"23-06\”}"

Dunno if that helps point you towards a solution to your problem.

All the best

Simon



On 15 Nov 2016, at 15:20, Jörgen Stahle <email@hidden> wrote:

Hi!

I’m an applescripter with no professional knowledge of other programming. And with a bad knowledge of the right terms to use here. Anyway, her is a try to describe my problem 

In a current project, I need a script to search a huge json for properties with certain names while ignoring most of the others. The properties are named by their key value, while the script only know this name as a string passed to it as a parameter. 

To convert the json into an applescript record, I can use a script library that I once got from Shane Stanley in this forum that (thanks again, Shane!!) But now I want to be able to refer to a certain properties without the need to loop all the json data – since the json is so huge and I have to do that kind of query many times in a time critical situation.


So – is there a solution out there that I can use to search dynamically for json properties from applescript?

Simplified example:

I want to be able to ask for the property ”lateprime” among the two properties under ”timeblocksDefs” in the small json below:



{"timeblocksDefs": {
   
"normal": {
       
"morning": "06-09",
       
"day": "09-18",
       
"primetime": "18-22",
       
"nighttime": "22-06"
   
},
   
"lateprime": {
       
"morning": "06-09",
       
"day": "09-19",
       
"primetime": "19-23",
       
"nighttime": "23-06"
   
}
}}


In my real case, the json is of course far more complicated…  Any suggestions welcome!




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Dynamic search for json properties
      • From: Shane Stanley <email@hidden>
References: 
 >Re: Dynamic search for json properties (From: Simon Forster <email@hidden>)

  • Prev by Date: Re: Dynamic search for json properties
  • Next by Date: Re: Dynamic search for json properties
  • Previous by thread: Re: Dynamic search for json properties
  • Next by thread: Re: Dynamic search for json properties
  • Index(es):
    • Date
    • Thread