Live data from Hacker News

Select Transform: JSON template over JSON

selecttransform.github.io

21–30 of 76 posts

Re: Select Transform: JSON template over JSON

#21
post #18
post #14

I think there's a lot of useful unexplored territory in this sort of tool. So much work nowadays is happening in maps/dictionaries/what-have-you, with a lot of work being, essentially, data shape transformation. Despite this, many languages aren't great at this! we have tools that work alright on first-order operations but fall apart on higher level things. Even really simple things like "conditionally include a key"…

Since you're using Python for your example, it's worth mentioning that 3.5 extended the unpacking syntax [1] to sort of do what you want. foo = { 'a': 1, 'b': 2, **({ 'c': 3, 'd': 4, } if bar else {}), } This adds `c: 3` and `d: 4` into `foo` conditional on `bar`. [1] https://docs.python.org/3/whatsnew/3.5.html#pep-448-addition...

oh, this is most definitely a thing I want. Not sure if my coworkers would appreciate this however. Would still have liked a bit of a simpler syntax for inclusion but this could be worth the ugliness

Nice that this works on lists as well.

Re: Select Transform: JSON template over JSON

#23
post #21
post #18

Earlier quoted context omitted.

Since you're using Python for your example, it's worth mentioning that 3.5 extended the unpacking syntax [1] to sort of do what you want. foo = { 'a': 1, 'b': 2, **({ 'c': 3, 'd': 4, } if bar else {}), } This adds `c: 3` and `d: 4` into `foo` conditional on `bar`. [1] https://docs.python.org/3/whatsnew/3.5.html#pep-448-addition...

oh, this is most definitely a thing I want. Not sure if my coworkers would appreciate this however. Would still have liked a bit of a simpler syntax for inclusion but this could be worth the ugliness Nice that this works on lists as well.

Yeah, I personally find this sort of syntax convolution ugly and hard to read but I thought it relevant. Enough rope to hang yourself with and whatnot.

Re: Select Transform: JSON template over JSON

#24
post #14

I think there's a lot of useful unexplored territory in this sort of tool. So much work nowadays is happening in maps/dictionaries/what-have-you, with a lot of work being, essentially, data shape transformation. Despite this, many languages aren't great at this! we have tools that work alright on first-order operations but fall apart on higher level things. Even really simple things like "conditionally include a key"…

>How many times have we all written

A decent amount, but I'd rather have all of that separate because it makes the code easier to read.

I'm not interested in how concise a block of JSON text can be with all of my transformations of conditionals and loops. That's going to be difficult to read without highlighting. I also can't put a breakpoint in the middle of some JSON text without the appropriate support from the language/runtime/IDE.

However, this syntax is much better than JSON Patch which I've considered using for document templates.

Re: Select Transform: JSON template over JSON

#25
post #6

I like the fact that this exists, but I prefer to use jq. I also would like to know in which situation this is preferable to jq.

There's also http://jmespath.org -- the syntax for which is supported in AWS CLIs. Not sure which one is better at this point. Using jq still often feels like trying to decode a code golf contest entry.

Really? I find jq syntax rather pleasant to read and write.

Re: Select Transform: JSON template over JSON

#26

XSLT for JSON?

I could imagine use cases for this for example for REST apis that consume json and then have to transform it generically to some other json for db storage in a document database. The internal storage could change over time with the outside spec staying the same, and such transforms could be used to do the mapping per version. This particular implementation looks a bit messy though - XSLT is valid XML and Xpath, while…

> That's more human readable, but also slightly inconsequent - you wouldn't be able to validate such a template with json schema for example.

Could you elaborate?

Re: Select Transform: JSON template over JSON

#27

Hi, my name is Ethan. I'm the creator. I thought I would provide some context on why I wrote this library, and how I'm using this right now. So here it goes: Other than ST.js, I also work on another open source project called Jasonette ( https://www.jasonette.com ), which lets you write an entire native iOS/Android app in nothing but JSON markup. And when you can express the entire app logic--from model to view to co…

So Jasonette functions are just about transforming the model, all in a reactive way? Like view->events->transform model->view?

Re: Select Transform: JSON template over JSON

#28
post #14

I think there's a lot of useful unexplored territory in this sort of tool. So much work nowadays is happening in maps/dictionaries/what-have-you, with a lot of work being, essentially, data shape transformation. Despite this, many languages aren't great at this! we have tools that work alright on first-order operations but fall apart on higher level things. Even really simple things like "conditionally include a key"…

IMHO any tool that converts JSON to JSON is pointing to the wrong direction.

Json should strictly be used as a serialization format and everyone who touches it should immediately parse it into a typed data-structure that represents your domain better.

In fact you really should represent your data in your code using whatever native object hierarchy / algebraic data types / option types / etc. your language supports. (Algebraic datatypes are awesome at this sort of thing, it allows you to avoid the aforementioned conditionality)

The problem is the right way to do this is also the hard way, because people hate writing "boilerplate" classes and serialization code and once you start thinking about making it "easy", you end up using json as the in-application data structure because "who needs types anyway".

Re: Select Transform: JSON template over JSON

#30
post #27

Hi, my name is Ethan. I'm the creator. I thought I would provide some context on why I wrote this library, and how I'm using this right now. So here it goes: Other than ST.js, I also work on another open source project called Jasonette ( https://www.jasonette.com ), which lets you write an entire native iOS/Android app in nothing but JSON markup. And when you can express the entire app logic--from model to view to co…

So Jasonette functions are just about transforming the model, all in a reactive way? Like view->events->transform model->view?

It can transform anything that's written in JSON, which means both model and view and anything else.

One of the use cases for ST.js in Jasonette is dynamic client-side rendering, where it takes dynamic data and renders it against the template, after which it becomes native components.

Another use case is implementing actual functional programming. This one is not as straight-forward since it involves maintaining another separate state machine for function call stack. But this is also achieved using ST templates. Here's a blog post that talks about it in detail http://blog.jasonette.com/2017/02/15/functional-programming-... but it's a bit involved.

Also Jasonette has something called mixins, which you probably can guess based on its name. It lets you mix in JSON objects into another, so that you can make the app modular. That's also achieved internally using ST.

Overall, I believe even I'm just scratching the surface of what this can do, which is why I decided to take some time to clean things up and prepare and open it up as its own repo, because I think there's tons of other things that can be done by taking advantage of the fact that all that's happening is:

JSON A + JSON B = JSON C

Hope this makes sense!

Post reply on HN