0

parseJSON

Dear all,

 

anybody who can tell me what I do wrong here :

I have a text file with the following content :

{
"invoices[
{

"recipient": "23022",
"date": "",
"credit period": 8,
"message":"Faktura ",
"our reference": "Martin Reslow",
"purchase order": "",
"currency": "DKK",
"exchange rate": "1",
"rounding": true,
"template": "Df",
"show delivery date": true,
"show delivery address": true,
"items":[
{

"product": "1"
"comment": "et eller andet",
"quantity": "1",
"unit": "",
"price": "1350",
}
]
}
]
}

In a formula field I have the following script : parseJSON(textfile) - but nothing happens, and according to the manual for this function the file is invalid if it gives no result.

 

I would be extremely happy if someone can guide me here - how do I create this JSON file, which I by the way will import to an accounting system.

Thanks

Leo

22 replies

null
    • Fred
    • 2 days ago
    • Reported - view

    Are you trying to get Ninox to create a JSON file?

      • Leo_Woer
      • 2 days ago
      • Reported - view

       Yes I am - and the file has been approved by the company who made the accounting system.  and Steven - in fact I have a file just exactly alike the one I send, which works perfectly  - but as a JSON object, but cab be read in a text editor. I enclose it just to show you

       

      rgds

      Leo

      • Fred
      • 2 days ago
      • Reported - view

       can you post your code in Ninox that creates the json?

      • Leo_Woer
      • yesterday
      • Reported - view

       Hi fred yes thank you for looking in to this - here is my button :

      let Xkonto := 1000;
      let Xkredit := first((select Indstillinger).Kreditdage);
      let Xmodkonto := 2000;
      let Xkurs := 100;
      let Xtype := "Manuel faktura";
      let Xkunde := Kundenavn;
      let Xknr := (select Kunder where Kundenavn = Xkunde).'Kundenr.';
      let Xpris := round(number(('Aftalt pris' + 'Total timepris -tilbagegang' + 'Total materialepris excl. moms') *
          1.25), 2);
      let Yday := day(Fakturadato) + Xkredit;
      let Xfaktdato := day(Fakturadato) + "." + month(Fakturadato) + "." + year(Fakturadato);
      let Xfaktnr := 'Fakturanr.';
      let Zdato := date(Fakturadato) + Xkredit;
      let Xdate := format(Fakturadato, "YYYY-MM-DD");
      let Xfaktforfald := day(Forfaldsdato) + "." + month(Forfaldsdato) + "." + year(Forfaldsdato);
      'JSON-indhold' := null;
      'JSON-fil' := null;
      let Ystart := "{" + """invoices" + ":" +
          "[
      " +
          "{ ";
      let Ymidt := """items"":" +
          "[
      " +
          "{ " +
          " ";
      let Yslut := "}
      ]
      }
      ]
      }";
      let Xrecipient := """recipient" + """" + ":" + " " + """" + Xknr + """" + ",";
      let Ydate := """date" + """" + ":" + " " + """" + Xdate + """" + ",";
      let Ycredit := """credit period" + """" + ":" + " " + Xkredit + ",";
      let Ymsg := """message" + """" + ":" + """" + "Faktura " + """" + ",";
      let Yref := """our reference" + """" + ":" + """Martin Reslow" + """" + ",";
      let Ypurch := """purchase order" + """" + ":" + " " + """""" + ",";
      let Yvaluta := """currency" + """" + ":" + " " + """DKK""" + ",";
      let Ykurs := """exchange rate" + """" + ":" + " " + """1""" + ",";
      let Yround := """rounding" + """" + ":" + " " + "true" + ",";
      let Ytemplate := """template" + """" + ":" + " " + """Df""" + ",";
      let Ydelivery := """show delivery date" + """" + ":" + " " + "true" + ",";
      let Ydeladr := """show delivery address" + """" + ":" + " " + "true" + ",";
      let Yprod := """product""" + ":" + " " + """1""" + ",";
      let Ycomment := """comment""" + ": ""et eller andet" + """" + ",";
      let Yquan := """quantity" + """" + ":" + " " + """1""" + ",";
      let Yunit := """unit" + """" + ":" + " " + """""" + ",";
      let Yprice := """price" + """" + ":" + " " + """" + Xpris + """";
      let Ycsv := Ystart + Xrecipient + Ydate + Ycredit + Ymsg + Yref + Ypurch + Yvaluta + Ykurs +
          Yround +
          Ytemplate +
          Ydelivery +
          Ydeladr +
          Ymidt +
          Yprod +
          Ycomment +
          Yquan +
          Yunit +
          Yprice +
          Yslut;
      'JSON-indhold' := Ycsv;
      'JSON-fil' := createTextFile(this, 'JSON-indhold', "manfak.json")

      • Leo_Woer
      • yesterday
      • Reported - view

       Hi again Fred, I discovered some errors in the script so here is the latest. My textfile looks exactly (apparently) as the file Steven send m - which works with the parse, but the textfile I create with this get not accepted by the parse function, and I haven't still found out have to send the the parsed file when it works.

       

      let Xkonto := "1000";
      let Xkredit := text(first((select Indstillinger).Kreditdage));
      let Xmodkonto := "2000";
      let Xkurs := "100";
      let Xtype := "Manuel faktura";
      let Xkunde := Kundenavn;
      let Xknr := text((select Kunder where Kundenavn = Xkunde).'Kundenr.');
      let Xpris := text(round(number(('Aftalt pris' + 'Total timepris -tilbagegang' + 'Total materialepris excl. moms') *
          1.25), 2));
      let Yday := text(day(Fakturadato) + Xkredit);
      let Xfaktdato := text(day(Fakturadato) + "." + month(Fakturadato) + "." + year(Fakturadato));
      let Xfaktnr := text('Fakturanr.');
      let Zdato := text(date(Fakturadato) + Xkredit);
      let Xdate := text(format(Fakturadato, "YYYY-MM-DD"));
      let Xfaktforfald := text(day(Forfaldsdato) + "." + month(Forfaldsdato) + "." + year(Forfaldsdato));
      'JSON-indhold' := null;
      'JSON-fil' := null;
      'Text 3' := null;
      let Ystart := "{" + """invoices""" + ":" + "[" + "{";
      let Ymidt := """items"":" + "[" + "{";
      let Yslut := "}]}]}";
      let Xrecipient := """recipient""" + ":" + """" + Xknr + """" + ",";
      let Ydate := """date""" + ":" + """" + Xdate + """" + ",";
      let Ycredit := """credit period""" + ":" + Xkredit + ",";
      let Ymsg := """message""" + ":" + """Faktura""" + ",";
      let Yref := """our reference""" + ":" + """Martin Reslow""" + ",";
      let Ypurch := """purchase order""" + ":" + """""" + ",";
      let Yvaluta := """currency""" + ":" + """DKK""" + ",";
      let Ykurs := """exchange rate""" + ":" + """1""" + ",";
      let Yround := """rounding""" + ":" + " " + "true" + ",";
      let Ytemplate := """template""" + ":" + """Df""" + ", ";
      let Ydelivery := """show_delivery_date""" + ":" + "true" + ",";
      let Ydeladr := """show_delivery_address""" + ":" + "true" + ",";
      let Yprod := """product""" + ":" + """1""" + ",";
      let Ycomment := """comment""" + ":" + """et eller andet""" + ",";
      let Yquan := """quantity""" + ":" + """1""" + ",";
      let Yunit := """unit""" + ":" + """""" + ",";
      let Yprice := """price""" + ":" + """" + Xpris + """";
      let Ycsv := Ystart + Xrecipient + Ydate + Ycredit + Ymsg + Yref + Ypurch + Yvaluta + Ykurs +
          Yround +
          Ytemplate +
          Ydelivery +
          Ydeladr +
          Ymidt +
          Yprod +
          Ycomment +
          Yquan +
          Yunit +
          Yprice +
          Yslut;
      'JSON-indhold' := Ycsv;
      'Text 3' := Ycsv;
      'JSON-fil' := createTextFile(this, 'JSON-indhold', "manfak.json")

    • Ninox partner
    • RoSoft_Steven.1
    • 2 days ago
    • Reported - view

    Well, the first line doesn't seem to be right: { "invoices":[ 

      • Leo_Woer
      • 2 days ago
      • Reported - view

       Yes you are right I forgot the (":), but even after correction I still get no result with the parseJSON function - any idea ?

      • Ninox partner
      • RoSoft_Steven.1
      • 2 days ago
      • Reported - view

       The JSON is still not valid: You "product":1", is missing a comma and there is also a comma too much at the end:

      this is your working JSON:

      {
      "invoices":[
      {  "recipient": "23022",
      "date": "",
      "credit period": 8,
      "message":"Faktura ",
      "our reference": "Martin Reslow",
      "purchase order": "",
      "currency": "DKK",
      "exchange rate": "1",
      "rounding": true,
      "template": "Df",
      "show delivery date": true,
      "show delivery address": true,
      "items":[
      {  "product": "1",
      "comment": "et eller andet",
      "quantity": "1",
      "unit": "",
      "price": "1350"
      }
      ]
      }
      ]
      }

      With this formula you can get items from your JSON:

      parseJSON(yourJSON).first(invoices).recipient

      to get the recipient. (Just be aware, once you have arrays in your json, you need to specify which item you need, here i took the first.

      Tip the use of spaces in the keynames is also not recommended, better use underscore e.g.:

      show_delivery_date

       

      EDIT: Just tested with your encloded "test.json" and this seems to be OK..... 

      • Leo_Woer
      • 2 days ago
      • Reported - view

       Thank you Steven so much - I will try it immediately and return with result

      rgds

      Leo

      • Leo_Woer
      • 2 days ago
      • Reported - view

       Hi Steven

      it woks pretty good I get an image field to have the JSONB object now  as 2[object Object], however, when trying to say save as in the image menu Ninox breaks immediately and don't save the file, I have tried several times with no luck. Then I tried to send the object as an attachment in mail, but this doesn't work because it says that the file doesn't exists.

      Do you know how to save or send this file correctly (I would like to have it saved as ManInv.json

      Sorry to bother you again with this

      rgds

      Leo

    • Ninox partner
    • RoSoft_Steven.1
    • yesterday
    • Reported - view

      can you put this in a formula field and check if you get a right JSON or any errors?

    let Xkunde := Kundenavn;
    this.{
        invoices: [ {
            recipient: text((select Kunder where Kundenavn = Xkunde).'Kundenr.'),
            date: text(format(Fakturadato, "YYYY-MM-DD")),
            credit_period: text(first((select Indstillinger).Kreditdage)),
            message: "Faktura",
            our_reference: "Martin Reslow",
            purchase_order: "",
            currency: "DDK",
            exchange_rate: 1,
            rounding: true,
            template: "Df",
            show_delivery_date: true,
            show_delivery_address: true,
            items: [ {
                product: 1,
                comment: "et eller andet",
                quantity: 1,
                unit: "",
                price: text(round(number(('Aftalt pris' + 'Total timepris -tilbagegang' + 'Total materialepris excl. moms') * 1.25), 2)),
            }],
    }]}
    

    Maybe if you gave us more info about the used tables and the fields and the relationships, we can finetune your json. 

      • Leo_Woer
      • yesterday
      • Reported - view

       Hi Steven - it looks correct in a formula field, but it fails when trying to parse it.

      a little more information :

      I have a button where I create the json file, which is on basis of information I also use for the invoice. This information is :

      customer number

      Invoice date

      Credit time

      our reference

      besides the text, which I hardcode (message, Comment,)

      i also should be able to read more lines in the invoice, which resides in a table called invoice lines and set it in via a loop. Right now I only work with one line, to get it working.

      the end result should be send as a json object to the issuing comany's account manager, to import it in Economacs - and the test file works perfectly in this, which I have been sent from aderstedt software.

      The total result should be, that when I can do this, I will approach all the craftsmen in DK who mount kitchens, as this system is developed for my customer for this, and I have been promised from the kitchen company, that I will get contact info of all their connected craftsmen doing this. the sytem in short saves the craftsman several hours a month, because he directly fro his phone create an offer, and he can stand on place an do the documentation (as photo from phone) which the kitchen company require and at the same time send the invoice also from his phone - the last brick in the puzzle is at the same time to send the json file. 

      Hope this gives you the info you need - and I am really happy with both yours an Fred's involvement here.

      regards

      Leo

      • Leo_Woer
      • yesterday
      • Reported - view

       Hi again - I saw that content of quantity missed "", I have now corrected it but still no parsing - I then set it in to my button and created JSON-indhold (content), and here it occurs as an object, so I guess this is what you wanted - or ??

      • Ninox partner
      • RoSoft_Steven.1
      • yesterday
      • Reported - view

       If Kundeavn is a relation field to your client then you can shorten the code like this. Also to get more items using a loop, you can use the following code. In my opinion, the code is more readable this way...

      Sidenote: if you want to specify a number like quantity,these numbers are not nessesarely to be between double quotes ""  in a json. Also the values true or false and null don't need to be between "". 

      Like  said, if you put this in a formula field, then you can refer to this formula field in other code-pieces, so the database would be easier to "debug" and maintain if you need to adapt the JSON.

      I also use this method to connect to the Peppol network with API btw.

      this.{
          invoices: [ {
              recipient: text(Kundenavn.'Kundenr.'),
              date: text(format(Fakturadato, "YYYY-MM-DD")),
              credit_period: text(first((select Indstillinger).Kreditdage)),
              message: "Faktura",
              our_reference: "Martin Reslow",
              purchase_order: "",
              currency: "DDK",
              exchange_rate: 1,
              rounding: true,
              template: "Df",
              show_delivery_date: true,
              show_delivery_address: true,
              items: 'invoice lines'.{
                  product: 1,
                  comment: "et eller andet",
                  quantity: 1,
                  unit: "",
                  price: text(round(number(('Aftalt pris' + 'Total timepris -tilbagegang' + 'Total materialepris excl. moms') * 1.25), 2)),
              },
      }]}
      • Leo_Woer
      • yesterday
      • Reported - view

       the problem is that I can't import it in Economacs if I don't have the double quotes - Economacs says that the item is missing if I don't have them. Also to @Fred - I tried the formatJson, it doesn't work because it then add the underscore, and unfortunately I cannot createTextFile from a Formula - so any idea of how to copy the formula content over in a textfile ?

      I have tried to place the script in a button, but that again replaces the blank with an underscore

      • Ninox partner
      • RoSoft_Steven.1
      • yesterday
      • Reported - view

       

      I have folowing code in a button, where i have the json in a formula field "FactuurJSON" and a image field "JSONimagefield" :

      let txt := formatJSON(FactuurJSON);
      JSONimagefield := createTextFile(this, txt, "test.json")

      The file test.json is created in this image field.

      Then if i add a multiline text field "TXTfactuurJSON" and add this code to the last line in my button, i also have the JSON as text in my multiline text field:

      TXTFactuurJSON := formatJSON(FactuurJSON);

      even

      TXTFactuurJSON := text(FactuurJSON) 

      is also working.

      • Leo_Woer
      • yesterday
      • Reported - view

       Hi Steven - thanks that helped, however, I had to change your button regarding the exchange rate to the following :

      let txt := formatJSON('Dannelse af JSON fil til economacs');
      let txt2 := replace(txt, "exchange_rate", "exchange rate");
      'JSON-fil' := createTextFile(this, txt2, "test1.json")

      (exchange_rate was the only thing that Economacs couldn't accept  - and I have tried to put this in single quotes with no luck, but above works)

      and by downloading this it works perfectly - so now I just need to send it as attachment in a mail - I tried to send JSON-fil as attachment, but Ninox will not accept to send it, so now I just need to solve this. Should you have any idea of why Ninox will not send it as sendmail - please tell me.

      Until now a million thanks for the help from both of you. 

       

      rgds

      Leo

    • Fred
    • yesterday
    • Reported - view

    A couple of things I've learned as I expand my use of JSON in Ninox:

    1) if you create a JSON in Ninox, like in  example, then you don't need to parse it. It is already in a valid JSON format. You can use the formula field name in other formulas.

    2) if you want to create a JSON then display it in a text field, then you need to use formatJSON(). This converts the JSON into a string. Which is what you need to use to export it into a file if you use  method. 

    3) You only need to use parseJSON() if you JSON data in a text format that you need to convert into a machine readable JSON format.

      • Leo_Woer
      • yesterday
      • Reported - view

       Thank You very much Fred - I will try this immediately and give you the result.

      To you and  I used you script and set the necessary "" in - likewise I needed to remove the underscores in your file, and then I took a paste copy of the formulas content and set it in a textfile where I gave it the extension .json. Now when I import this file in Economacs it just works, so now I will do a format to a textfile, and see if this also works. Will keep you both updated.

      Fred You have helped me before with a loop - how would you set up the loop if the invoice have more than one line the invoice line are children to the invoice.

      rgds

      Leo

    • Ninox partner
    • RoSoft_Steven.1
    • yesterday
    • Reported - view

    So to finetune your json code more, if you want to use spaces instead of the underscores_ you must put your key names between single quotes, also you need to convert all the numbers to text, using the text() function like you use for the item price.

    this.{
        invoices: [ {
            recipient: text(Kundenavn.'Kundenr.'),
            date: text(format(Fakturadato, "YYYY-MM-DD")),
            'credit period': text(first((select Indstillinger).Kreditdage)),
            message: "Faktura",
            'our reference': "Martin Reslow",
            'purchase order': "",
            currency: "DDK",
            'exchange rate': "1",
            rounding: "true",
            template: "Df",
            'show delivery date': "true",
            'show delivery address': "true",
            items: ['invoice lines'.{
                product: "1",
                comment: "et eller andet",
                quantity: "1",
                unit: "",
                price: text(round(number(('Aftalt pris' + 'Total timepris -tilbagegang' + 'Total materialepris excl. moms') * 1.25), 2)),
            }],
    }]}

    be aware that if you format your numberfield, you sometimes get the numberformatting back if you only use text() function. Meaning you get 5.500,00 DDK for example. To avoid this you can use the number() function within the text() function. e.g.: text(number(unit_price)) In this case you get "5500" 

    • Leo_Woer
    • yesterday
    • Reported - view

    that is valid information - thanks

    • Leo_Woer
    • 23 hrs ago
    • Reported - view

    apparently the send problem was overload at Ninox - now every thing works

    rgds

    Leo

Content aside

  • Status Answered
  • 23 hrs agoLast active
  • 22Replies
  • 53Views
  • 3 Following