Page 2 of 2

Re: Using variables in script to create Json - Newbie help

Posted: Tue Feb 06, 2018 3:30 pm
by Padawan
Switch uses [ and ] characters to indicate the beginning and end of Switch variables. I believe the issue is caused because the square brackets which are used to indicate the json array are confusing switch because it thinks they indicate a Switch variable, while they aren't.

It is possible to escape the square brackets by doubling the opening bracket. In my tests the following works:

Code: Select all

{
"Items": [[{
      "StockItemID": 13185,
      "Quantity": "0001",
      "Ref1": "[Metadata.Text:Path="/root/amount",Dataset="Xml",Model="XML"]"
        }]
}

Re: Using variables in script to create Json - Newbie help

Posted: Tue Feb 06, 2018 11:24 pm
by jan_suhr
The square brackets around the variables are not the problem. Switch will send the value of the variable to the app. If you run messages with the debug option on you will see how Switch give each variable a value, if it gets wrong there it is something wrong with the variable or with the JSON surrounding the variable.

Re: Using variables in script to create Json - Newbie help

Posted: Wed Feb 07, 2018 11:30 am
by jan_suhr
There is a problem with the Square brackets [ ] when used in the Make JSON. Switch has a special use of square brackets and that conflicts with JSON's use of square brackets. And just to escape them doesn't solve it.

I will create a workaround to solve this problem so that we can have correct JSON with JSON arrays working with Switch variables.

Hopefully it will be ready next week on the App Store.

Re: Using variables in script to create Json - Newbie help

Posted: Wed Feb 07, 2018 8:41 pm
by Redshift
Thanks for both your help.

Re: Using variables in script to create Json - Newbie help

Posted: Mon Feb 19, 2018 3:53 am
by Redshift
jan_suhr wrote:There is a problem with the Square brackets [ ] when used in the Make JSON. Switch has a special use of square brackets and that conflicts with JSON's use of square brackets. And just to escape them doesn't solve it.

I will create a workaround to solve this problem so that we can have correct JSON with JSON arrays working with Switch variables.

Hopefully it will be ready next week on the App Store.
Hi Jan,

I'm getting an error on the new app which is showing a "red cog" as well.

I'm trying the same code as earlier (have tried others as well) and getting a warning "Error in line 36 of script : Error. Trying to access undefined member '0'" (screen shot attached)

Is this something in the app at all?

Cheers,
Cade

Re: Using variables in script to create Json - Newbie help

Posted: Mon Feb 19, 2018 6:44 am
by jan_suhr
Can you send me the JSON you are trying with.

I did try the one you posted earlier without any variables and it did work as is, viewtopic.php?f=13&t=2056#p7200



Jan

Re: Using variables in script to create Json - Newbie help

Posted: Mon Feb 19, 2018 1:45 pm
by jan_suhr
Well it seems that if you only have one variable from Switch the App will fail, if you have more than one variable it works.

I'm working on a fix.

Re: Using variables in script to create Json - Newbie help

Posted: Tue Feb 20, 2018 2:07 am
by Redshift
I think I have got it working outside of that, the app didn't like switch adding [X] inside variables to separate data under similar headings from the XML.

Any reason why it reverses the JSON?

e.g.

Code: Select all

{
  "Orders": [{
    "UniqueSubmitOrderID": "string",
    "Status": {
      "Value": 0,
      "EnumType": "string"
    },
    "InvoiceCustomer": {
      "Code": "LEXNEX",
      "Contact": "API Invoice Contact",
      "ContactEmail": "nz.api@printing.com"
  },
    "DeliveryCustomer": {
      "Name": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/Name",Dataset="Xml",Model="XML"]",
      "Address": {
"AddressLines": ["[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street1",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street2",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street3",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street4",Dataset="Xml",Model="XML"]"],
         
"City": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/City",Dataset="Xml",Model="XML"]", 
        "Region": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/City",Dataset="Xml",Model="XML"]",
        "Country": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Country",Dataset="Xml",Model="XML"]",
        "Postcode": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/PostalCode",Dataset="Xml",Model="XML"]"
 },
      "Contact": "API Delivery Contact",
      "ContactEmail": "nz.api@printing.com"   
},
     "Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/@orderID",Dataset="Xml",Model="XML"]",
"Items": {
"StockItemID": "13185",
"Quantity": "[Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/@quantity",Dataset="Xml",Model="XML"]",
"Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/ItemDetail/Description",Dataset="Xml",Model="XML"]"
}
  }]
}
is coming out as

Code: Select all

{"Orders":[{"DeliveryCustomer":{"Address":{"AddressLines":["Auckland District Law Society","Attn: Richard Ormiston","2 Chancery St","undefined"],"City":"Auckland","Country":"NEW ZEALAND","Postcode":"1140","Region":"Auckland"},"Contact":"API Delivery Contact","ContactEmail":"nz.api@printing.com","Name":"The Law Society Store"},"InvoiceCustomer":{"Code":"LEXNEX","Contact":"API Invoice Contact","ContactEmail":"nz.api@printing.com"},"Items":{"Quantity":"000000005","Ref1":"Dobbie's 6th ed","StockItemID":"13185"},"Ref1":"D00645022","Status":{"EnumType":"string","Value":0},"UniqueSubmitOrderID":"string"}]}

Re: Using variables in script to create Json - Newbie help

Posted: Tue Feb 20, 2018 2:53 am
by Redshift
So I'm getting an error again when adding another array in, it seems to be working fine in the address area but failing in the below part. If I remove it it's fine and gives the result above but when I add in [] it spits back a "unterminated object" in the debug notes again.

Any clues?

adding in the array into this part after items which is what my API the JSON is going into requires:

Code: Select all

},
     "Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/@orderID",Dataset="Xml",Model="XML"]",
"Items": [{
"StockItemID": "13185",
"Quantity": "[Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/@quantity",Dataset="Xml",Model="XML"]",
"Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/ItemDetail/Description",Dataset="Xml",Model="XML"]"
}
]


Final Code with the Array i'm trying is

Code: Select all

{
  "Orders": [{
    "UniqueSubmitOrderID": "string",
    "Status": {
      "Value": 0,
      "EnumType": "string"
    },
    "InvoiceCustomer": {
      "Code": "LEXNEX",
      "Contact": "API Invoice Contact",
      "ContactEmail": "nz.api@printing.com"
  },
    "DeliveryCustomer": {
      "Name": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/Name",Dataset="Xml",Model="XML"]",
      "Address": {
"AddressLines": ["[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street1",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street2",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street3",Dataset="Xml",Model="XML"]", "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Street4",Dataset="Xml",Model="XML"]"],
         
"City": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/City",Dataset="Xml",Model="XML"]", 
        "Region": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/City",Dataset="Xml",Model="XML"]",
        "Country": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/Country",Dataset="Xml",Model="XML"]",
        "Postcode": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/ShipTo/Address/PostalAddress/PostalCode",Dataset="Xml",Model="XML"]"
 },
      "Contact": "API Delivery Contact",
      "ContactEmail": "nz.api@printing.com"   
},
     "Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/OrderRequestHeader/@orderID",Dataset="Xml",Model="XML"]",
"Items": [{
"StockItemID": 13185,
"Quantity": [Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/@quantity",Dataset="Xml",Model="XML"],
"Ref1": "[Metadata.Text:Path="/cXML/Request/OrderRequest/ItemOut/ItemDetail/Description",Dataset="Xml",Model="XML"]"
}
]
  }]
}

Re: Using variables in script to create Json - Newbie help

Posted: Tue Feb 20, 2018 7:19 am
by jan_suhr
Well as you say it becomes problematic when there are a lot of square brackets in the code.

I did some testing yesterday with some advanced variabels that have several square brackets inside of the variables and it creates problems.

To get around this problem we do some regexp search and replace to escape the brackets and that works with simpler variables now. But when we get in to more advanced variables it gets problematic since we don't want to escape the JSON square brackets.

It is all dependent on the JSOSN parser that we now have in Switch 17 update 2 and I am considering an option to skip the parser to just get a text file with JSON code that is not parsed and it will be up to you to make it correct.

I'll keep you posted.

Re: Using variables in script to create Json - Newbie help

Posted: Tue Mar 06, 2018 4:44 am
by Redshift
Hi Jan,

Have you had any luck with this one at all?

Cheers,
Cade

Re: Using variables in script to create Json - Newbie help

Posted: Tue Mar 06, 2018 7:30 am
by jan_suhr
There is a new version since a few weeks that should have all the problems fixed.

Re: Using variables in script to create Json - Newbie help

Posted: Wed Mar 07, 2018 4:58 am
by Redshift
Thanks Jan I'll continue to test. IN first instance it is giving me the same errors as a above but I'll try start through elimination again.