1 2 Previous Next 15 Replies Latest reply on Jan 27, 2016 3:22 PM by michaelkirwan

    help with parsing

    michaelkirwan

      Hi All

      Parsing has never been my strong point. Below is a response from an api request. The format is always the same but the information (in red) varies in length. I wonder could someone help parse the 3 pieces of information in red. Thank you in advance...

       

       

      {"valid":true,"database":"ok","format_valid":true,"query":"IE6562566R","country_code":"IE","vat_number":"6562566R","company_name":"PHONEWATCH LTD","company_address":"MARK PHILIPS ,UNIT 3\/4 SANDYFORD PARK ,SANDYFORD INDUSTRIAL ESTATE ,DUBLIN 18"}

        • 1. Re: help with parsing
          Mike Duncan

          The base elements plugin can help with handling responses in JSON, which is what this look like. There is also a solution over at modular filemaker you might look at if you require a solution that does not use plugins.

           

          Mike

          • 2. Re: help with parsing
            michaelkirwan

            Thanks Mike....  but I cant use a plugin as it has to work on an iPad.

            • 4. Re: help with parsing
              ch0c0halic

              Michael,

               

              The pattern is a comma followed by a quote is the end of each parameter. So first we separate them into lines and then we get the second parameter of each line as the value.

               

              Script looks like this:

              (You would use the field and not this data in the real script).


              Set Variable ( $data ; 

              Substitute ( {"valid":true,"database":"ok","format_valid":true,"query":"IE6562566R","country_code":"IE","vat_number":"6562566R","company_name":"PHONEWATCH LTD","company_address":"MARK PHILIPS ,UNIT 3\/4 SANDYFORD PARK ,SANDYFORD INDUSTRIAL ESTATE ,DUBLIN 18"} ; ["{" ; ""] ; ["}" ; ""] )

              )

              //remove unwanted "{" and "}" characters


              Set variable ( $line_data ; Substitute ( $data ; ",\"" ; "¶" )

               

              Set Variable ( $valid ; Leftwords ( GetValue ( $linedata ; 1 ) ; 1 )

               

              Set variable ( $company_name_line ; Substitute ( GetValue ( $linedata ; 7 ) ; ["\":\"" ; "¶"] ; ["\"" ; ""] )

              //separate name and value onto separate lines and remove unwanted quotes.

              Set variable ( $company_name ; GetValue ( $company_name_line ; 2 )

               

              Set variable ( $company_address_line ; Substitute ( GetValue ( $linedata ; 8 ) ; ["\":\"" ; "¶"] ; ["\"" ; ""] )

              //separate name and value onto separate lines and remove unwanted quotes.

              Set variable ( $company_address ; GetValue ( $company_address_line ; 2 )

              • 5. Re: help with parsing
                user19752

                There is missing " in start of 1st Substitute().

                 

                Does this work if data ends with comma?

                {"name":"value,","nextname":"othervalue,,"}

                • 6. Re: help with parsing
                  Extensitech

                  I have a custom function that's helpful (I think) for things like this. I have more specific cf's for json, but this one's kind of all purpose:

                   

                  x_txt_middle_from_to ( text ; from ; to ) =

                   

                  /*

                  ------------------------------------------------------------------------------------------------

                  Purpose: Returns the text between two strings.

                   

                  Parameters:

                    text = the whole text

                    from = the text after which the target text begins

                    to = the text before which the target text ends

                   

                  Dependencies:

                   

                  Example: x_txt_middle_from_to ( "this, that and this other thing¶are makin' me crazy" ; "that" ; "thing" ) returns " and this other "

                   

                  Notes:

                  This function does NOT take into account nested occurrences of from and to. For that, see x_txt_parse_span

                  ------------------------------------------------------------------------------------------------

                  */

                  Let ( [

                  x=1

                  /*

                  //testing

                  ; text = "this, that and this other thing¶are makin' me crazy"

                  ; from = "that"

                  ; to = "thing"

                  */

                   

                  ; p1a = Position ( text ; from ; 1 ; 1 )

                  ; p1 = p1a + Case ( p1a > 0 ; Length ( from ) )

                  ; p2a =  Position ( text ; to ; p1a + Length ( from ) ; 1 )

                  ; p2 = Case ( p2a > 0 ; p2a ; Length ( text ) + 1  )

                  ; text = Case ( p2 < p1 ; Right ( text ; Length ( text ) - p2 ) ; text )

                  ; result = Case ( p1a ; Middle ( text ; p1  ; p2 - p1  ) )

                   

                  ] ;

                   

                  result

                   

                  )

                   

                  ------------------------

                   

                  x_txt_middle_from_to ( YourText ; "valid\":"; ",") = true

                  x_txt_middle_from_to ( YourText ; "Company_name\":\""; quote ( "," ) ) = PHONEWATCH LTD

                  x_txt_middle_from_to ( YourText  ; "Company_address\":\""; "\"}" ) = MARK PHILIPS ,UNIT 3\/4 SANDYFORD PARK ,SANDYFORD INDUSTRIAL ESTATE ,DUBLIN 18

                   

                  HTH

                   

                  Chris Cain

                  Extensitech

                  • 7. Re: help with parsing

                    Use a substitution and the create a list.

                     

                    set var ( string ;substitute (

                    {"valid":true,"database":"ok","format_valid":true,"query":"IE6562566R","country_code":"IE","vat_number":"6562566R","company_name":"PHONEWATCH LTD","company_address":"MARK PHILIPS ,UNIT 3\/4 SANDYFORD PARK ,SANDYFORD INDUSTRIAL ESTATE ,DUBLIN 18"} ; "," ; "PP" )  // PP is the pilcrow )

                     

                    Now use the text functions to get the specific list item and string will look like:

                     

                    garbage...

                    Phone Watch

                    Mark Pillips

                    Unit etc

                    Sandyford Industrial...

                    Dublin 18

                     

                    set whatever ( GetValue (  string ; X)

                     

                    Now you have  just 5 or so sets and its easy...

                     

                    Company = 7 or so

                    Name = 8

                    Unit = 9

                    xxx

                    county = 10

                    • 8. Re: help with parsing
                      ch0c0halic

                      The replace of a comma with a return will also separate the pieces of the address. I used a comma and a quote because its unique to the end of each name-value pair, replace this ",\"" with a return.

                       

                      MARK PHILIPS ,UNIT 3\/4 SANDYFORD PARK ,SANDYFORD INDUSTRIAL ESTATE ,DUBLIN 18

                       

                      becomes

                       

                      MARK PHILIPS

                      UNIT 3\/4 SANDYFORD PARK

                      SANDYFORD INDUSTRIAL ESTATE

                      DUBLIN 18

                      • 9. Re: help with parsing
                        ch0c0halic

                        Its actually missing quotes around the whole string. But that's why he would use his field and not my typing. (Which is even worse with spell check off).

                        • 10. Re: help with parsing
                          Mike Duncan

                          Jimmy,

                           

                          This approach may work depending on the JSON returned. Say you need to address a particular node, then it may be more difficult to get the correct result. This example looks pretty flat, so should be OK. Just fyi

                           

                          Thanks

                          Mike

                          • 11. Re: help with parsing
                            michaelkirwan

                            Hi ch0c0halic

                             

                             

                             

                            I just got back from a business trip so I'm sorry for the delay in getting back to you. Thanks for your input into this. I am very appreciative to you for giving me a solution. Thanks again... I'm humbled by the time an effort you gave me...

                             

                            Michael

                            • 12. Re: help with parsing
                              michaelkirwan

                              Hi Mike

                               

                              I just got back from a business trip so I'm sorry for the delay in getting back to you. Thanks for your time input into this. I followed your link and looked at the videos.... Many thanks.

                               

                              Michael

                              • 13. Re: help with parsing
                                michaelkirwan

                                Hi... I just got back from a business trip so I'm sorry for the delay in getting back to you. Thanks for your input, I appreciate it.

                                • 14. Re: help with parsing
                                  michaelkirwan

                                  Hi Chris

                                   

                                  I just got back from a business trip and saw your reply. I am very appreciative to you for giving me a solution. I'm flattered by the amount of time you gave me... thanks a bunch....!!

                                  1 2 Previous Next