JSON: add a block based on a given condition
Hello.
I am creating an XML file and would like to start with a JSON because it offers greater readability and ease of maintenance. In the structure of the JSON how can I decide to add blocks determined by an if statement.
In the example below:
Text1: {
@: example
},
Text2: {
@: blabla
}
I would like the Text2 block to be processed only if a certain condition is true:
Text1: {
@: example
},
if myCondition = true then
Text2: {
@: blabla
}
end
What is the correct syntax to get this result?
Thank you in advance.
8 replies
-
Have you seen this post?
-
I would make the formula/codeblock in a text-format (where you can use the if...then) and then use the eval() function to execute the code.
-
Thank you guys.
I did some tests. When I use the conditional approach (line #10), the JSON code works correctly, that is, it shows or not the block according to the if statement:
However, when I convert this code to XML the code does not work correctly. I show you an example:
let a := null; { ElectronicInvoice: { ElectronicInvoiceHeader: { TransmissionDatas: { TransmitterId: { CountryId: { @: "IT" }, FiscalCode: if a then { @: "FISCAL CODE" } end }, ProgressiveSending: { @: "10" }, TransmissionFormat: { @: "FPR12" }, RecipientCode: { @: "0000000" } } } } }
JSON: result is fine, it does not show the undefined key:
{"ElectronicInvoice":{"ElectronicInvoiceHeader":{"TransmissionDatas":{"TransmitterId":{"CountryId":{"@":"IT"}},"ProgressiveSending":{"@":"10"},"TransmissionFormat":{"@":"FPR12"},"RecipientCode":{"@":"0000000"}}}}}
XML: I am shown the corresponding row, even though it is not valorized:
<?xml version="1.0" encoding="utf-8"?> <ElectronicInvoice> <ElectronicInvoiceHeader> <TransmissionDatas> <TransmitterId> <CountryId>IT</CountryId> <FiscalCode></FiscalCode> </TransmitterId> <ProgressiveSending>10</ProgressiveSending> <TransmissionFormat>FPR12</TransmissionFormat> <RecipientCode>0000000</RecipientCode> </TransmissionDatas> </ElectronicInvoiceHeader> </ElectronicInvoice>
-
. Thank you! Great stratagem. Do you think this is a reportable bug or a natural behavior of the JSON?
Content aside
- Status Answered
- 1 yr agoLast active
- 8Replies
- 272Views
-
4
Following