Using variables in script to create Json - Newbie help

Padawan
Member
Posts: 142
Joined: Mon Jun 12, 2017 8:48 pm

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

Post by Padawan » Tue Feb 06, 2018 3:30 pm

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"]"
        }]
}

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Tue Feb 06, 2018 11:24 pm

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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Wed Feb 07, 2018 11:30 am

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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Wed Feb 07, 2018 8:41 pm

Thanks for both your help.

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Mon Feb 19, 2018 3:53 am

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
Attachments
json error.JPG
json error.JPG (30.67 KiB) Viewed 1914 times

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Mon Feb 19, 2018 6:44 am

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
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Mon Feb 19, 2018 1:45 pm

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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Tue Feb 20, 2018 2:07 am

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"}]}

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Tue Feb 20, 2018 2:53 am

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"]"
}
]
  }]
}

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Tue Feb 20, 2018 7:19 am

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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Tue Mar 06, 2018 4:44 am

Hi Jan,

Have you had any luck with this one at all?

Cheers,
Cade

jan_suhr
Member
Posts: 164
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Post by jan_suhr » Tue Mar 06, 2018 7:30 am

There is a new version since a few weeks that should have all the problems fixed.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

Redshift
Newbie
Posts: 13
Joined: Thu Dec 21, 2017 2:40 am

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

Post by Redshift » Wed Mar 07, 2018 4:58 am

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.

Post Reply