JSON To Structure (Composition by smokris)

Author: smokris
License: MIT
Date: 2010.02.15
Compatibility: 10.5, 10.6
Categories:
Required plugins:
(none)

Converts a JSON string input into a Structure.

Useful in conjunction with the Kineme FileTools "String With URL" patch, to produce an alternative to the built-in XML Importer patch.

An example composition is attached, using this patch to parse the results of a Yahoo Search API query.

PreviewAttachmentSize
JSON To Structure Demo.qtz12.76 KB
JSON To Structure.qtz1.83 KB

cybero's picture
Re: JSON To Structure (Composition by smokris)

Sorry to be the bearer of sad tidings about something that is pretty brilliant - but this is provoking a JavaScript related GF Log message

[12:22:28.295]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:22:28.295]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 31.336
[12:23:09.637]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:23:09.637]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 0.000
[12:23:29.988]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:23:29.988]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 0.000
[12:24:20.922]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:24:20.922]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 0.000
[12:24:45.330]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:24:45.330]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 0.000
[12:25:14.400]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> SyntaxError: Unable to parse JSON string
[12:25:14.400]   <QCJavaScript = 0x17E989E0 "JavaScript_1"> Execution failed at time 0.000

when the search term has a space - only single word search terms not multiple word strings. a multiple search string will not obtain a search result at all.

The error clears upon presenting a only a single word search term or even character, so not especially serious.

Update - entering terms as say ford/motors providing an escape character by means of the slash character makes this work without a JS error.

Just been tinkering with the Demo comp and obviously enough one can add in additional search boxes [Arguments] to contain discrete terms that build to be the total string. - does work.

usefuldesign.au's picture
Re: JSON To Structure (Composition by smokris)

I'm on Leopard. Put the JSON_To_Structure.qtz into …/Library/Graphics/Quartz Composer Patches/

When I open JSON_To_Structure_Demo comp it doesn't find the JSON_To_Structure patch. ie Console Error. Manually placing the JSON_To_Structure patch into the comp and joining it to Kineme String With URL and ResultSet patches generates a structure but the same image is being repeated 10 times.

The text gives the different URLs though.

PreviewAttachmentSize
picforkineme.jpg
picforkineme.jpg12.58 KB

cybero's picture
Re: JSON To Structure (Composition by smokris)

This works on .5 you say ? looks like it isn't reading the JSON output correctly at all apart from the text listing, it just isn't leveraging the underlying .6.2 specific JSON and structure stuff - pity. probably could be 'reverse engineered' to do the same, although virtual macro wise isn't that also .6.x specific? difficult to tell from your mini picture :-) so I've attached my results for your comparison , same misspelt beafheart •~ I do get a range of different text and associated image results, but then I'm running .6.x, so no surprise really. do you also get a js error when presenting a spaced search term, like manchester united whereas manchester/united obtains no error and gets results [by the looks of things , just the textual search results]?

PreviewAttachmentSize
beafheart.jpg
beafheart.jpg39.39 KB

gtoledo3's picture
Re: JSON To Structure (Composition by smokris)

I'm in the most recent 10.6 seed right now, and it's working fine. I had to open the "JSON_To_Structure.qtz", let QC install it as a Virtual Macro (it prompts for this), then open the demo qtz.

This is excellent. So simple and elegant.

I'll try this in 10.5 too. I've had probs with Virtual Macro's from 10.6 not working correctly in 10.5 before...

cybero's picture
Re: JSON To Structure (Composition by smokris)

Are you saying that multiple word search terms like 'pancake day' would work without needing to enter them as pancake/day, as I'm having to do right now?

gtoledo3's picture
Re: JSON To Structure (Composition by smokris)

Oh no, I didn't mean to infer that. The slash is needed for me as well, when I use multiple words.

smokris's picture
Re: JSON To Structure (Composition by smokris)

Hm, there are a few different issues at play here:

  • The Demo requires Kineme FileTools. The Clip itself doesn't require FileTools.
  • The Demo requires 10.6 (because the virtual macro is embedded, and because Image Importer doesn't work inside Iterators in 10.5). The Clip itself doesn't require 10.6.
  • Queries with multiple search terms don't work because spaces (and some other characters) need to be escaped when placed into a URL string. (Sorry, the demo was just a quick 5-minute-hack to show a possible use of the JSON decoder, not meant to be a perfect example of best practices or anything.) I've updated the composition to escape URL characters now.

usefuldesign.au's picture
Re: JSON To Structure (Composition by smokris)

Thanks for clearing that up (10.6), smokris.

usefuldesign.au's picture
Re: JSON To Structure (Composition by smokris)

"Beefheart" gave me an image that was too Lame to post x10 (the heart of a beef)!

cybero's picture
Re: JSON To Structure (Composition by smokris)

Thanks for listening and responding .

smokris's picture
Re: JSON To Structure (Composition by smokris)

Updated: this clip might actually run on 10.5 now --- apparently 10.5's Javascript patch doesn't include the JSON class (while 10.6's does). I added a workaround so that it works on both.

cradle's picture
Re: JSON To Structure (Composition by smokris)

Thankyou very much for being in the google index for quartz composer JSON - you saved me a lot of time :D

photonal's picture
Re: JSON To Structure (Composition by smokris)

Here's a slight hack to utilise the ANU Quantum Random Number Server.

After parsing the number is fed to an OSC output for wiring to another app.

PreviewAttachmentSize
Random Numbers 256.qtz5.95 KB
Random Numbers 65535.qtz5.95 KB

gtoledo3's picture
Re: JSON To Structure (Composition by smokris)

Is it possible for this to still work? The URL content isn't passing… is it non-valid, or is this known to not work? I'm trying to do something pretty silly, so it's not particularly pressing, but I'm curious.

smokris's picture
Re: JSON To Structure (Composition by smokris)

@gtoledo3, are you referring to JSON To Structure Demo.qtz? Unfortunately, Yahoo shut down their image search API a few years ago, so that particular example won't work anymore.

But if you find another search engine API that returns a list of images as JSON, I'd expect Kineme String With URL and JSON To Structure to still work.

gtoledo3's picture
Re: JSON To Structure (Composition by smokris)

I see, thanks for the feedback.

Does anyone currently know a good/best way to get some images from the web into qc? I note that coge WebKit seems to do this but it seems to not show gifs in the returned images, and maybe sometimes images that are embedded certain ways? Maybe this is not true for newer versions (that I may not have). I tried the rss w/image importer but was having problems- mainly in finding a valid RSS feed I know has valid images.

incubo's picture
Re: JSON To Structure (Composition by smokris)

Hi,

Do you know if this still works on Sierra (10.12.3)? For me it for some reason doesn't seem to work. I use Kineme HTTP Query to get data from Facebook Graph api, and I do get the JSON, but the /json to structure patch doesn't seem to eat any data.

Does anyone have an alternate way to parse JSON in QC?

Achim Breidenbach's picture
Re: JSON To Structure (Composition by smokris)

I don't know about the KineMe patch but we have developed some other patches for JSON that might be of interest for you:

https://github.com/boinx/QCJSON

incubo's picture
Re: JSON To Structure (Composition by smokris)

Thank you! I actually found your JSON patches earlier today and they gave me hope that JSON parsing is still possible in QC. But unfortunately your patches don't help me as is, because I can't make calls to Facebook Graph Api without sending some query parameters (Access Token, App ID, App Secret). What would be perfect, is a patch that simply takes JSON in and outputs a QC structure.

Here is a sample of the JSON (my latest posts), that FB sends. Parsing this with tools like String Component is a mess.

{"posts":{"data":[{"message":"Minna Canth ja tasa-arvo: kymmenen vuotta yleisen\u00e4 liputusp\u00e4iv\u00e4n\u00e4. \u2764\ud83c\uddeb\ud83c\uddee","created_time":"2017-03-19T11:52:29+0000","id":"10154530412168990_10154595824848990"},{"message":"Toteutimme Ia Enster\u00e4n ja Taija Raution kanssa pikkupikku installaation Lapinlahden sairaalaan. Tule avajaisiin tai k\u00e4y katsomassa kev\u00e4tk\u00e4velyn p\u00e4\u00e4tteeksi joskus my\u00f6hemmin.","story":"Aku Meril\u00e4inen shared Pro Lapinlahti ry's event.","created_time":"2017-03-17T17:14:53+0000","id":"10154530412168990_10154590940023990"}

smokris's picture
Re: JSON To Structure (Composition by smokris)

@incubo, it looks like sometime in the last few years, the behavior of Quartz Composer's JavaScript patch has changed. The QC documentation says:

The script always gets executed first for error check with all input ports set to null. Check the _testMode global boolean to detect that initial pass.

…but that's no longer accurate — the _testMode value is always true; it no longer gets set to false like it used to. So I think you can make it work by changing the if(_testMode) to something like if(JSONString).

By the way, in Vuo we recently added support for JSON and XML parsing, generation, search (content/attribute-based + XPath), and manipulation.

incubo's picture
Re: JSON To Structure (Composition by smokris)

That did the trick – thank you very much!

I really need to start the transition to Vuo for multiple reasons. Perhaps finally after this project. The JSON and XML parsing has been crucial for me and I'm glad to hear that you have implemented it now. You do super work!