Announcement

Collapse
No announcement yet.

How do I test if element exists in JSON object?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I test if element exists in JSON object?

    I have JSON object and need to test if element exists.
    This element will have empty string if exists.

    Code:
    { 
        "XAVResponse": {
            "Response": {
                "ResponseStatus": {
                    "Code": "1",
                    "Description": "Success"
                },
                "TransactionReference": {
                    "CustomerContext": "Verify that an XAV request with an Address Line 1 element returns a successful response."
                }
            },
            "AmbiguousAddressIndicator": "",
            .
            .
            .
        }
    }
    Element "
    AmbiguousAddressIndicator" will appear with empty string if needed.

    Thank you in advance.
    Daniel Kim, Compu-Mate
    Developer

    #2
    You can use miva_json_decode and miva_member_exists to do this. Something like the following should work.

    PHP Code:
    <MvIF EXPR "{ NOT miva_json_decode( l.json, l.data ) }">
        <
    error handling here>
    </
    MvIF>

    <
    MvIF EXPR "{ miva_member_exists( l.data:XAVResponse, 'AmbiguousAddressIndicator' ) }">
        <
    the element exists>
    </
    MvIF
    David Carver
    Miva, Inc. | Software Developer

    Comment


      #3
      That did the trick.

      Thanks.
      Daniel Kim, Compu-Mate
      Developer

      Comment


        #4
        How do I find what type the element is?
        miva_array_elements does not give me array dimension when it is an object instead of an array.
        miva_array_elements on Candidate element returns 2 for the first case.

        Code:
        {
            "Candidate": {
                "AddressKeyFormat": {
                    "AddressLine": "2215 GESSNER RD",
                    "PoliticalDivision2": "HOUSTON",
                    "PoliticalDivision1": "TX",
                    "PostcodePrimaryLow": "77080",
                    "PostcodeExtendedLow": "5003",
                    "Region": "HOUSTON TX 77080-5003",
                    "CountryCode": "US"
                }
            }
        }
        or

        Code:
        {
            "Candidate": [{
                "AddressKeyFormat": {
                    "AddressLine": "2215 GESSNER RD",
                    "PoliticalDivision2": "HOUSTON",
                    "PoliticalDivision1": "TX",
                    "PostcodePrimaryLow": "77080",
                    "PostcodeExtendedLow": "5003",
                    "Region": "HOUSTON TX 77080-5003",
                    "CountryCode": "US"
                }
            },
            {
                "AddressKeyFormat": {
                    "AddressLine": "2215 GESSNER RD",
                    "PoliticalDivision2": "HOUSTON",
                    "PoliticalDivision1": "TX",
                    "PostcodePrimaryLow": "77080",
                    "PostcodeExtendedLow": "5003",
                    "Region": "HOUSTON TX 77080-5003",
                    "CountryCode": "US"
                }
            }]
        }
        Thanks.
        Daniel Kim, Compu-Mate
        Developer

        Comment


          #5
          Take the array and use MvRevealstructure

          https://www.mivascript.com/item/miva...STRUCTURE.html
          M.A.D.* since 1997

          http://www.scotsscripts.com

          *miva application developers

          Comment


            #6
            Thanks Scot,

            It is JSON object and I am parsing the result.
            I am trying to find return result is single object or array of objects.
            Daniel Kim, Compu-Mate
            Developer

            Comment


              #7
              Oh, got it. Do what Carver said first:

              miva_json_decode( l.json, l.data )

              Then use miva_array_search(...) on l.data

              https://www.mivascript.com/item/miva...ay_search.html

              miva_Array_search is kind of tricky at first, but the examples are pretty good on the docs page.
              M.A.D.* since 1997

              http://www.scotsscripts.com

              *miva application developers

              Comment


                #8
                I do not know the element "Candidate" is array or not yet.
                I know what to do when it is array or object.

                Is there any function which finds the type of object?
                Daniel Kim, Compu-Mate
                Developer

                Comment


                  #9
                  One way is to use miva_array_serialize(...) on the array item. You can "see" what the names of the variables are this way.

                  After you decode the json and have l.data, do something like:

                  Code:
                  <MvFOREACH ITERATOR = "l.dataitem" INDEX = "l.dataitempos" ARRAY = "l.data">
                     <MvASSIGN NAME = "l.ok" VALUE = "{ miva_array_serialize(l.dataitem) }" />
                     <MvEVAL EXPR = "{ glosub(l.dataitem,',','<br>')}">
                  <br>
                  </MvFOREACH>
                  You can parse l.item_serialized to find the var names you are looking for.
                  Last edited by Scot - ScotsScripts.com; 10-10-23, 04:00 PM. Reason: Brain fart- meant miva_array_serialize instead of encodeentities
                  M.A.D.* since 1997

                  http://www.scotsscripts.com

                  *miva application developers

                  Comment


                    #10
                    Referring back to post #4 (the numbers are along the right side of the page), it seems weird to me that miva_array_elements returned 2 for Candidate in the first example. I will guess that something isn't quite right in the way your code is handling the data.
                    If you run an object through miva_array_serialize, and then use glosub() to change the commas to <br/> tags, you can display it on your screen in an easy-to-read format that will allow you to see exactly what's in there.
                    If you post some of the code that's processing the JSON, some of us may be able to give more help.
                    Kent Multer
                    Magic Metal Productions
                    http://TheMagicM.com
                    * Web developer/designer
                    * E-commerce and Miva
                    * Author, The Official Miva Web Scripting Book -- available on-line:
                    http://www.amazon.com/exec/obidos/IS...icmetalproducA

                    Comment


                      #11
                      Here is the code.

                      Code:
                      <MIVA STANDARDOUTPUTLEVEL="html,text">
                      <MvCAPTURE VARIABLE = "l.response">
                      {
                          "XAVResponse":
                          {
                              "Response":
                              {
                                  "ResponseStatus":
                                  {
                                      "Code": "1",
                                      "Description": "Success"
                                  }
                              },
                              "ValidAddressIndicator": "",
                              "AddressClassification":
                              {
                                  "Code": "1",
                                  "Description": "Commercial"
                              },
                              "Candidate":
                              {
                                  "AddressClassification":
                                  {
                                      "Code": "1",
                                      "Description": "Commercial"
                                  },
                                  "AddressKeyFormat":
                                  {
                                      "AddressLine": "2215 GESSNER RD",
                                      "PoliticalDivision2": "HOUSTON",
                                      "PoliticalDivision1": "TX",
                                      "PostcodePrimaryLow": "77080",
                                      "PostcodeExtendedLow": "5003",
                                      "Region": "HOUSTON TX 77080-5003",
                                      "CountryCode": "US"
                                  }
                              }
                          }
                      }
                      <//MvCAPTURE>
                      <MvASSIGN NAME = "l.ok" VALUE = "{ miva_json_decode(l.response, l.json_response) }">
                      <MvEVAL EXPR = "{ 'Number of candidates : ' $ miva_array_elements( l.json_response:xavresponse:candidate ) }"><br /><br />
                      <MvEVAL EXPR = "{ l.json_response:xavresponse:candidate }"><br /><br />
                      This block of codes will generate

                      Code:
                      Number of candidates : 2
                      
                      1,Commercial,2215 GESSNER RD,US,TX,HOUSTON,5003,77080,HOUSTON TX 77080-5003
                      Thanks
                      Last edited by compumate99; 10-11-23, 09:59 AM.
                      Daniel Kim, Compu-Mate
                      Developer

                      Comment


                        #12
                        That does seem strange, that the first MvEVAL returns 2. It's even stranger that the second MvEVAL returns nothing; is that what actually happened, or did you just not include that part in your post?
                        If this was my project, I'd put in some code to check the return value from miva_json_decode, to see if it's encountering an error. I'd also try the above mentioned trick to serialize l.json_response, to see exactly what it contains.
                        Possibly this is a bug in miva_array_elements. If so, a work-around would be, when you want to know the structure of a variable, call miva_struct_members first, to see if it's an object. Don't call miva_array_elements unless miva_struct_members returns zero.
                        Hope that helps --
                        Kent Multer
                        Magic Metal Productions
                        http://TheMagicM.com
                        * Web developer/designer
                        * E-commerce and Miva
                        * Author, The Official Miva Web Scripting Book -- available on-line:
                        http://www.amazon.com/exec/obidos/IS...icmetalproducA

                        Comment


                          #13
                          I am sorry Kent, I missed output from second MvEVAL.

                          It returns

                          Code:
                          1,Commercial,2215 GESSNER RD,US,TX,HOUSTON,5003,77080,HOUSTON TX 77080-5003
                          I will try your suggestion later.

                          Thank you
                          Daniel Kim, Compu-Mate
                          Developer

                          Comment


                            #14
                            Following code was executed

                            Code:
                            <MvASSIGN NAME = "l.stru_count" VALUE = "{ miva_struct_members(l.json_response:xavresponse:ca ndidate, l.stru_array) }">
                            <MvEVAL EXPR = "{ 'Member Count : ' $ l.stru_count $ ' | Serialized: ' $ miva_array_serialize(l.stru_array) }"><br /><br />
                            And received the result.

                            Code:
                            Member Count : 2 | Serialized: [1]=AddressClassification,[2]=AddressKeyFormat
                            So this return value 2 matches with 2 returned from MIVA_ARRAY_ELEMENTS.

                            However, if I run this code for JSON object with element type ARRAY

                            Code:
                            "Candidate":[
                            {
                                "AddressKeyFormat":
                                {
                                    "PoliticalDivision2": "ROSWELL",
                                    "PoliticalDivision1": "GA",
                                    "PostcodePrimaryLow": "30076",
                                    "Region": "ROSWELL GA 30076",
                                    "CountryCode": "US"
                                }
                            },
                            {
                                "AddressKeyFormat":
                                {
                                    "PoliticalDivision2": "ROSWELL",
                                    "PoliticalDivision1": "GA",
                                    "PostcodePrimaryLow": "30075",
                                    "Region": "ROSWELL GA 30075",
                                    "CountryCode": "US"
                                }
                            },
                            {
                                "AddressKeyFormat":
                                {
                                    "PoliticalDivision2": "ROSWELL",
                                    "PoliticalDivision1": "GA",
                                    "PostcodePrimaryLow": "30077",
                                    "Region": "ROSWELL GA 30077",
                                    "CountryCode": "US"
                                }
                            }]
                            I receive

                            Code:
                            Member Count : 0 | Serialized:
                            It looks MIVA_STRUCT_MEMBERS does not operate correctly on JSON ARRAY.
                            Daniel Kim, Compu-Mate
                            Developer

                            Comment


                              #15
                              Very strange ... I would check the return value from miva_json_decode. FYI, if it returns zero, you can call miva_json_decode_last_error() to get an error message telling what the problem was.
                              I would also do the serialize trick on the entire JSON reply (l.json_response), to see exactly what it contains.
                              Kent Multer
                              Magic Metal Productions
                              http://TheMagicM.com
                              * Web developer/designer
                              * E-commerce and Miva
                              * Author, The Official Miva Web Scripting Book -- available on-line:
                              http://www.amazon.com/exec/obidos/IS...icmetalproducA

                              Comment

                              Working...
                              X