0

I need to iterate a json object.

i fund in documentation : https://docs.ninox.com/en/print/print-customization/repetitions/iterate-over-objects

 

ho to do, but i cant use this.

 

Any Help?

Have a nice day.

13 replies

null
    • Fred
    • 7 mths ago
    • Reported - view

    can you post your code?

      • CISOFT_Sarl
      • 7 mths ago
      • Reported - view

       Hello Fred,

      i have a json object like this :

      let myObject := 

        {
          myObject : {
            paul : '10',
            jack : '20',
            bob  : '30'
          }
        };

      And i need to iterate on every key/value.

      I thinking the json object ca be used like : item(myObject, i ) for the key value.

      I look in the documentation : 

      https://docs.ninox.com/en/print/print-customization/repetitions/iterate-over-objects

      we can use : {d.myObject[i].att} to get the attribute and {d.myObject[i].val} to get the value of this attribue or key.

      But cant understant how you can use this.

      • szormpas
      • 7 mths ago
      • Reported - view

          Hi, according to the Ninox documentation you can use the item() function with a json object like below:  

      let data := {
          firstName: "Steve",
          lastName: "Rogers"
          };
      item(data, "firstName")
      
      • CISOFT_Sarl
      • 7 mths ago
      • Reported - view

       Yes thank you for the information, but in documentation is mentionned: 
       

       

      Syntax

      item([any], number)

      item(JSON, number)

      item(JSON, string)

       

      and the second is : item(JSON, number) and i dont know to use that version : item(JSON, number)

      • Fred
      • 7 mths ago
      • Reported - view

      Just to clairfy, are you trying to use JSON in a formula or in a dynamic print page? I ask because your first post refers to dynamic printing. Then posts the item() command which is used in Ninox formula fields.

      • szormpas
      • 7 mths ago
      • Reported - view

        item(myJSON, key) To extract the value of a key-value pair of a given JSON object. The key might be a string or a number. 
      I think you can not iterate a json object using item function because the number refers to key, it’s not an index

    • CISOFT_Sarl
    • 7 mths ago
    • Reported - view

    in relation with this part of documentation:
    https://docs.ninox.com/en/print/print-customization/repetitions/iterate-over-objects

    it is important part of my code this section of documentation.
    ------

     

    I need to create a function for logging.
    i have a object : let myObject:=

     {
        myObject : {
          paul : '10',
          jack : '20',
          bob  : '30'
        }
      }


    i create a function :
    function createLog(object:any) do
           let mylog:="";
           let sep:="|";
           for i in range(0,cnt(object.log)) do
                  log:=log+"
                         "+ DateTime+sep+object.log[i].att+object.log[i].val+.......
           end;

    end

     

    Yes i can send a array, but i want play with json object.

      • Fred
      • 7 mths ago
      • Reported - view

      You are mixing up commands from the dynamic print engine and regular Ninox commands.

      You wrote:

      function createLog(object:any) do
             let mylog:="";
             let sep:="|";
             for i in range(0,cnt(object.log)) do
                    log:=log+"
                           "+ DateTime+sep+object.log[i].att+object.log[i].val+.......
             end;
      end
      

      so on line 6 you are using [i], which is dynamic printing code. Well to be more specific it is what Carbone uses to do their dynamic printing. You can't use this in regular Ninox commands.

      Question: what is the log field? a reference field? a formula field? another type?

    • szormpas
    • 7 mths ago
    • Reported - view

     is the code below what are you looking for?

    let Data := {
            myObject: [{
                    paul: "10"
                }, {
                    jack: "20"
                }, {
                    bob: "30"
                }]
        };
    join(for i in Data.myObject do
        text(Data.myObject.i)
    end, ",")
    

    the above inside a function field results in:

    {"paul":"10"},{"jack":"20"},{"bob":"30"}

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

    You can also use this code to get a list of your Json:

    let myobject := {
            myObject: {
                paul: 10,
                jack: 20,
                bob: 30
            }
        };
    for name, value in myobject.myObject do
        name + ":" + value
    end
    

    The result of this is:

    paul:10,jack:20,bob:30
    
      • szormpas
      • 7 mths ago
      • Reported - view

        great! I've learned something new today!

      So, we can loop inside a json object if we declare two labels (variables) one for the "name" and the other for the "value" in the "name/value" pairs.

      Is my understanding correct?

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

    Yes, correct.

      • CISOFT_Sarl
      • 7 mths ago
      • Reported - view

       thank you and i like this way. 

Content aside

  • 7 mths agoLast active
  • 13Replies
  • 275Views
  • 4 Following