Today we have a tip for you without using MBS FileMaker Plugin and in pure FileMaker. Let's say you like to query current records in your found set as JSON, how would you do this?
First you need the list of the primary keys and we like to use a While() for this. For older FileMaker versions, you could use FM.Loop instead. The loop goes over found set and just queries primary key fields for all records in found set:
While ( [ liste = ""; i = 0 ] ; i < Get(FoundCount) ; [ i = i + 1; liste = GetNthRecord ( FileMaker Ideas::PrimaryKey ; i ) & "¶" & liste ] ; liste )
We could store this in $Keys and then continue with building the request, which needs to include the layout to use and the query, which looks for all the records by ID.
I like the concept, but I would tend to use a slightly different approach to building the request as I really dislike escaping double quotes - it makes code so much harder to read.
I am always torn between having to have a special layout to limit the fields returned using the DataAPI and using ExecuteSQL which doesn't easily return data in JSON format.
I would understand if you would not wish to do so from a "purity" point of view, but one variation that I see to the code above would be to iteratively build up ~result using string concatenation, rather than JSONSetElement, and then do any final tidying up of ~result in the final section of the while.
My motivation for suggesting this is just to get a little performance increase out of each iteration.
My MO for doing this sort of thing is usually to:
Start with an empty result string
Make sure that each item I am appending is a properly formed JSON object
Append each item with a comma prepended
After the loop, remove the initial leading comma (which is unwanted), then then surround the result in square brackets to create a proper JSON array.
Note that the Replace function works nicely for this last step, i.e.,
I don't object at all to string concatenation, I just dislike having to escape double quotes. I find it harder to read the code and also to debug. Perhaps a custom function to create "name":"value" would help me.
Thanks for considering this. No escaped quotes required. I don't like that either, and I don't recommend trying to roll JSON by hand, as it is not robust enough for my liking.
Thanks for the reply and thoughts, about JSONQuery @apjcs.
Occasionally, I conclude that I made JSONQuery a little too feature-rich for its own good, thus increasing everyone's need to go back to the examples more than I wish were necessary. There are two sides to that, of course -- if it had a more concise feature set, would it still be helpful enough. Either way, I appreciate reading the above comment. Thank you.