0

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

null
    • Fred
    • 9 mths ago
    • Reported - view

    Have you seen this post?

      • Ninox developer
      • Fabio
      • 9 mths ago
      • Reported - view

      Hi  .

      Yes, but at this stage I am in the process of building the JSON and I would like to include in the structure (if possible) only the necessary blocks based on case histories in the respective Ninox record.

      If I understand correctly, through the removeItem() function I could remove the unnecessary blocks, but first I should provide a complete structure. But it is complex and expensive in terms of implementation because the final structure for each record contains approximately 10% of all possible combinations of blocks 🤔.

      • Ninox developper
      • Jacques_TUR
      • 9 mths ago
      • Reported - view

      You can use setItem function to update your JSON structure step by step : https://docs.ninox.com/en/script/function-overview/functions/setitem.
       

      You can also use if to define the value of the key. If the value is undefined, the key is not stored in the JSON :

      var a := "jacques";
      var b := null;
      var myJSON := {
         name : if a then a end,
         address : if b then b end };
      myJSON;

      Result :

      {"name":"jacques"}

    • Ninox partner
    • RoSoft_Steven.1
    • 9 mths ago
    • Reported - view

    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.

    • Ninox developer
    • Fabio
    • 9 mths ago
    • Reported - view

    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>
    
      • Ninox developper
      • Jacques_TUR
      • 9 mths ago
      • Reported - view

        Indeed, as we can see by using the debug function with Ninext, there is still a trace of "FiscaleCode" in the JSON even if it doesn't appear when Ninox converts it to text for display.

      The solution is therefore to convert the JSON to text, then convert that text back to JSON before passing it to XML :

      let a := null;
      var j := {
          ElectronicInvoice: {
              ElectronicInvoiceHeader: {
                  TransmissionDatas: {
                      TransmitterId: {
                          CountryId: {
                              @: "IT"
                          },
                          FiscalCode: if a then
                              {
                                  @: "FISCAL CODE"
                              }
                          end
                      },
                      ProgressiveSending: {
                          @: "10"
                      },
                      TransmissionFormat: {
                          @: "FPR12"
                      },
                      RecipientCode: {
                          @: "0000000"
                      }
                  }
              }
          }
      };
      formatXML( parseJSON( formatJSON( j ) ) ,true)
      

      result : 

      <?xml version="1.0" encoding="utf-8"?>
      <ElectronicInvoice>
          <ElectronicInvoiceHeader>
              <TransmissionDatas>
                  <TransmitterId>
                      <CountryId>IT</CountryId>
                  </TransmitterId>
                  <ProgressiveSending>10</ProgressiveSending>
                  <TransmissionFormat>FPR12</TransmissionFormat>
                  <RecipientCode>0000000</RecipientCode>
              </TransmissionDatas>
          </ElectronicInvoiceHeader>
      </ElectronicInvoice>
      
    • Ninox developer
    • Fabio
    • 9 mths ago
    • Reported - view

    . Thank you! Great stratagem. Do you think this is a reportable bug or a natural behavior of the JSON?

      • Ninox developper
      • Jacques_TUR
      • 9 mths ago
      • Reported - view

       In JavaScript, JSON has the same behavior.

Content aside

  • Status Answered
  • 9 mths agoLast active
  • 8Replies
  • 155Views
  • 4 Following