1 Reply Latest reply on May 10, 2017 9:44 AM by fmpdude

    New JSON commands


      I used FM16's new JSON functions and coupled with the new data viewer window which works wonders. When I tried using a certain API I got some errors, the exact particulars are not necessary. After some debugging I realised that the problem was the JSON created using FM's new JSONSetElement, would actually create the JSON with all the elements in alphabetic order!! This API uses both XML and JSON formats and needs the input from either format in the particular order specified. I think the reason is that XML was used initially which required the format in a certain schema order and then the JSON implementation was adopted based on that.


      Anyhow this is what I got from FileMaker Pro 16 Help at the very bottom of the page:

      The JSON parser preserves the order of elements in an array, but not the order of elements in an object. Therefore, the JSON functions may return elements in an object in a different order from the order specified.


      It would be nice if the new commands didn't do any re-ordering!

        • 1. Re: New JSON commands

          Wow. The issue you raised, as I understand it, is why I tend to keep this type of coding outside FMP since I don't have full control in FMP. The code below shows how to substitute the default JSON data structure to preserve order. Additionally, the link below has several references on JSON. Note that by default (at least with Java's use of a regular HashMap), ordering is not guaranteed. If you NEED to order elements, you can use a different type of Map object during the JSON processing.


          java - Keep the order of the JSON keys during JSON conversion to CSV - Stack Overflow




          Here's some Java code that demonstrates some options:


          public class JsonExample

            public static void main(String args[])


            JSONObject object = new JSONObject();

          // order not guaranteed with default JSONObject (uses HashMap)


            object.put("1", "foo");

            object.put("num", new Integer(100));

            object.put("balance", new Double(1000.21));

            object.put("is_vip", new Boolean(true));

            object.put("nickname", "FMP");


            System.out.println("\nUsing Regular JSONObject: " + object.toString());



            // Or, using a Map...


            // LinkedHashMap maintains a doubly-linked list so the order is predictable.

            Map obj=new LinkedHashMap();


            obj.put("num",new Integer(100));

            obj.put("balance",new Double(1000.21));

            obj.put("is_vip",new Boolean(true));


            StringWriter out = new StringWriter();

            JSONValue.writeJSONString(obj, out);

            String jsonText = out.toString();


            System.out.print("\nUsing a LinkedHashMap: " + jsonText + "\n");





          ORDER NOT PRESERVED: Using Regular JSONObject: {"1":"foo","balance":1000.21,"is_vip":true,"num":100,"nickname":"FMP"}


          YES, ORDER PRESERVED: Using a LinkedHashMap: {"1":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":"FMP"}




          HOPE THIS HELPS.