Page 1 of 1

Make JSON

Posted: Thu Jun 06, 2024 8:32 am
by Mark Casey
Hi All

I have recently upgraded my Switch installation from Spring 2020 to the latest Fall 2023

With that some of the Apps have been upgraded

Since then I had a MAKE JSON command that no longer works, firstly is it possible to roll back the App Version? and if not can anyone identify whats wrong. Reading the notes I think its something to do with the quote marks but not having any luck
Message I see from switch: JSON code failed SyntaxError: Unexpected token / in JSON at position 64

THIS IS MY CODE:

{
"items": [
{
"orderReference": "[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]",
"recipient": {
"address": {
"fullName": "[Database.Text:SQL="SELECT DeliveryContact FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"companyName": "[Database.Text:SQL="SELECT DeliveryCustomerName FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine1": "[Database.Text:SQL="SELECT DeliveryAddress FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine2": "",
"addressLine3": "",
"city": "[Database.Text:SQL="SELECT DeliveryTown FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"county": "",
"postcode": "[Database.Text:SQL="SELECT DeliveryPostcode FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"countryCode": ""
},
"phoneNumber": "",
"addressBookReference": ""
},
"sender": {
"tradingName": "Team Impression Ltd",
"phoneNumber": "01132724800",
"emailAddress": "markca@team-impression.com"
},
"packages": [
{
"weightInGrams": 400,
"packageFormatIdentifier": "parcel",
"customPackageFormatIdentifier": "",
"contents": [
{
"name": "",
"SKU": "",
"quantity": 1,
"unitValue": 0,
"unitWeightInGrams": 0,
"customsDescription": "",
"extendedCustomsDescription": "",
"customsCode": "",
"originCountryCode": "",
"customsDeclarationCategory": "none",
"requiresExportLicence": true
}
]
}
],
"orderDate": "[Database.Text:SQL="SELECT Datedb FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"plannedDespatchDate": "",
"specialInstructions": "",
"subtotal": 0,
"shippingCostCharged": 0,
"otherCosts": 0,
"customsDutyCosts": "",
"total": 0,
"currencyCode": "",
"postageDetails": {
"sendNotificationsTo": "sender",
"serviceCode": "CRL48",
"serviceRegisterCode": "",
"consequentialLoss": "",
"receiveEmailNotification": false,
"receiveSmsNotification": false,
"guaranteedSaturdayDelivery": false,
"requestSignatureUponDelivery": false,
"isLocalCollect": false,
"safePlace": "",
"department": "",
"AIRNumber": "",
"IOSSNumber": "g",
"requiresExportLicense": false,
"commercialInvoiceNumber": "",
"commercialInvoiceDate": "2022-08-24T14:15:22Z"
}
}
]
}

Re: Make JSON

Posted: Thu Jun 06, 2024 8:51 am
by jan_suhr
In the latest versions you can use replacement characters to be able to make an JSON array using variables in the code.

Switch uses the Square brackets to define the values from a variable, this conflicts with the JSON array Square brackets and because of that Switch gets confused about the variable values.

So when you define your JSON code in the app all array square brackets has to be replaced with something.
Snag_2f7d100.png
Snag_2f7d100.png (14.66 KiB) Viewed 7880 times
Then when the JSON code is generated Switch will first add the variables and then the proper array square brackets will be put in place. Then the code is parsed to validate it.

Re: Make JSON

Posted: Thu Jun 06, 2024 9:29 am
by Mark Casey
Still same issue, although I notice another warning message os spaces as below

ERROR - Spaces are not allowed in variable definition in 'JSON code'
ERROR - JSON code failed SyntaxError: Unexpected token X in JSON at position 67

This is the start of the JSON Code

{
"items": [
{
"orderReference": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/JobNo"]",
"recipient": {
"address": {
"fullName": "[Database.Text:SQL="SELECT DeliveryContact FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"companyName": "[Database.Text:SQL="SELECT DeliveryCustomerName FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine1": "[Database.Text:SQL="SELECT DeliveryAddress FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine2": "",
"addressLine3": "",
"city": "[Database.Text:SQL="SELECT DeliveryTown FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"county": "",
"postcode": "[Database.Text:SQL="SELECT DeliveryPostcode FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"countryCode": ""
},

Re: Make JSON

Posted: Thu Jun 06, 2024 9:35 am
by jan_suhr
the error you got is from parsing the JSON

The Switch variables must be in single quotes for Switch to add the value

Code: Select all

"orderReference": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/JobNo"]"
Try it this way, note the single quote around the variable

Code: Select all

"orderReference": '[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/JobNo"]'
Those single quotes will be replaced by the app before parsing and validation.

Re: Make JSON

Posted: Thu Jun 06, 2024 9:51 am
by Mark Casey
Hi Jan
Still same Error
JSON code failed SyntaxError: Unexpected token X in JSON at position 67

I did change the code to:
{
"items": [
{
"orderReference": '[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/JobNo"]',
"recipient": {
"address": {
"fullName": "[Database.Text:SQL="SELECT DeliveryContact FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"companyName": "[Database.Text:SQL="SELECT DeliveryCustomerName FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine1": "[Database.Text:SQL="SELECT DeliveryAddress FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"addressLine2": "",
"addressLine3": "",
"city": "[Database.Text:SQL="SELECT DeliveryTown FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"county": "",
"postcode": "[Database.Text:SQL="SELECT DeliveryPostcode FROM MainJobDetails WHERE JobNo = '[Metadata.Text:Path="/Job/JobNo",Dataset="Xml",Model="XML"]'",Connection="PRIMO"]",
"countryCode": ""
},
"phoneNumber": "",
"addressBookReference": ""
},

Re: Make JSON

Posted: Thu Jun 06, 2024 9:53 am
by jan_suhr
You still have double quotes around the variables

Re: Make JSON

Posted: Thu Jun 06, 2024 10:01 am
by jan_suhr
And I see that you haven't added the replacement characters like "items": #%# and the rest until #@# at the end of that array.

If you rename the app element in your flow with "debug_" at the start your JSON code will show up in the messages Debug section. This can help you sort out what is wrong in your code.