Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Parse JSON formatted field to create multiple records

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

    Parse JSON formatted field to create multiple records

    Hello Everyone

    We are using a SQL Active Link table to read orders being created in a web ecommerce platform. The data is stored in MySQL, and the structures is very flat.

    ONE ORDER = ONE ROW

    Essentially they have merged the typical ORDER HEADER table and ORDER DETAIL table into one ORDER table.

    The storage of the detail data including ITEM, QTY, PRICE, DESC, etc is contained in one MEMO field.
    We need to write a script that reads the memo field data into a variable, parses the data in the variable, and creates a new ORDER DETAIL row with each new product ordered.

    The Json legend of formatting is at http://www.json.org/

    the Array is shown via [ ]
    The Object is shown via { }

    The data example below should create 3 separate records

    [{"rowid":"3a3d205e3f77d0d10822a2a7e7acf83e","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Bianco","size":"ML","sku":"8300496004851"},"subtotal":"35.95"},{"rowid":"321578152208d3d15681a497403ed26c","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Noisette (Hazelnut)","size":"M","sku":"8300496004943"},"subtotal":"35.95"},{"rowid":"c0ab5b5101bbb85339ce1a73f0e7dcb5","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Nero (Black)","size":"M","sku":"8300496005117"},"subtotal":"35.95"}]
    Logical Design
    Keven Thibeault
    Boston, Massachusetts
    Dwell in possibility

    #2
    Re: Parse JSON formatted field to create multiple records

    See it these experiments get you started.

    Code:
    tbl = table.open("dtbl")
    tst = tbl.DT
    ? tst
    = [{"rowid":"3a3d205e3f77d0d10822a2a7e7acf83e","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Bianco","size":"ML","sku":"8300496004851"},"subtotal":"35.95"},{"rowid":"321578152208d3d15681a497403ed26c","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Noisette (Hazelnut)","size":"M","sku":"8300496004943"},"subtotal":"35.95"},{"rowid":"c0ab5b5101bbb85339ce1a73f0e7dcb5","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Nero (Black)","size":"M","sku":"8300496005117"},"subtotal":"35.95"}]
    
    ntest = strtran(tst,"{\"row",crlf()+"{\"row")
    ? ntest
    = [
    {"rowid":"3a3d205e3f77d0d10822a2a7e7acf83e","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Bianco","size":"ML","sku":"8300496004851"},"subtotal":"35.95"},
    {"rowid":"321578152208d3d15681a497403ed26c","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Noisette (Hazelnut)","size":"M","sku":"8300496004943"},"subtotal":"35.95"},
    {"rowid":"c0ab5b5101bbb85339ce1a73f0e7dcb5","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Nero (Black)","size":"M","sku":"8300496005117"},"subtotal":"35.95"}]
    nntest = ""
    cnt = w_count(ntest,crlf())
    ? cnt
    = 4
    for i = 2 to cnt
    #nntest = nntest+word(ntest,i,crlf())+crlf()
    #next i
    ? nntest
    = {"rowid":"3a3d205e3f77d0d10822a2a7e7acf83e","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Bianco","size":"ML","sku":"8300496004851"},"subtotal":"35.95"},
    {"rowid":"321578152208d3d15681a497403ed26c","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Noisette (Hazelnut)","size":"M","sku":"8300496004943"},"subtotal":"35.95"},
    {"rowid":"c0ab5b5101bbb85339ce1a73f0e7dcb5","id":"5","qty":1,"price":"35.95","name":"Magic Panty","options":{"color":"Nero (Black)","size":"M","sku":"8300496005117"},"subtotal":"35.95"}]
    
    ccnt = w_count(word(nntest,1,crlf()),",")
    ? ccnt
    = 9
    
    for i = 1 to 9
    #? word(word(nntest,1,crlf()),i,",")
    #next i
    {"rowid":"3a3d205e3f77d0d10822a2a7e7acf83e"
    "id":"5"
    "qty":1
    "price":"35.95"
    "name":"Magic Panty"
    "options":{"color":"Bianco"
    "size":"ML"
    "sku":"8300496004851"}
    "subtotal":"35.95"}
    There can be only one.

    Comment


      #3
      Re: Parse JSON formatted field to create multiple records

      Thanks Stan! will try em out!
      Logical Design
      Keven Thibeault
      Boston, Massachusetts
      Dwell in possibility

      Comment


        #4
        Re: Parse JSON formatted field to create multiple records

        The options "field" will require a little extra coding since it doesn't follow the pattern exactly.

        "options":{"color":"Bianco"

        I'd have to know what might be found here for options and what you would want to do with it to say more.
        There can be only one.

        Comment


          #5
          Re: Parse JSON formatted field to create multiple records

          yeah.. this options field is a weird one. the vision was to have the ability to have MULTIPLE numbers of options per product.
          BUT
          the reality is that OPTIONS should be thrown away, and just
          COLOR, SIZE, and SKU should be added to the detail table. playing with the scripts now.
          Logical Design
          Keven Thibeault
          Boston, Massachusetts
          Dwell in possibility

          Comment


            #6
            Re: Parse JSON formatted field to create multiple records

            Code:
            tbl = table.open("dtbl") 'the table holding the data
            tst = tbl.dt 'dt is the memo field
            tbl.close()
            ntest = strtran(tst,"{\"row",crlf()+"{\"row")
            nntest = ""
            cnt = w_count(ntest,crlf())
            FOR i = 2 TO cnt
            	nntest = nntest+word(ntest,i,crlf())+crlf()
            NEXT i
            lines = w_count(nntest,crlf())
            ccnt = w_count(word(nntest,1,crlf()),",")
            a_tbl = table.open("order") ' the table to write the orders to
            FOR l = 1 TO lines
            	FOR i = 1 TO ccnt
            		IF left(word(word(nntest,l,crlf()),i,","),5) = quote("sku")
            			a_tbl.enter_begin()
            			a_tbl.Sku = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
            			a_tbl.enter_end(.t.)
            		END IF
            	NEXT i
            	FOR i = 1 TO ccnt
            		IF left(word(word(nntest,l,crlf()),i,","),6) = quote("size")
            			a_tbl.change_begin()
            			a_tbl.Size = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
            			a_tbl.change_end(.t.)
            		else if	left(word(word(nntest,l,crlf()),i,","),9) = quote("options")
            			a_tbl.change_begin()
            			a_tbl.Color = unquote(word(word(word(nntest,l,crlf()),i,","),3,":"))
            			a_tbl.change_end(.t.)
            		END IF
            	NEXT i
            NEXT l
            a_tbl.close()
            Last edited by Stan Mathews; 03-09-2010, 04:00 PM.
            There can be only one.

            Comment


              #7
              Re: Parse JSON formatted field to create multiple records

              This works great, except, what is the approach to use to fill in ALL the data fields, not just the SKU, SIZE, and COLOR?
              I have played with the script with no progress unfortunately... Any ideas? We want to have one row per rowid, and all data elements filled in. cheers
              Logical Design
              Keven Thibeault
              Boston, Massachusetts
              Dwell in possibility

              Comment


                #8
                Re: Parse JSON formatted field to create multiple records

                Code:
                tbl = table.open("dtbl") 'the table holding the data
                tst = tbl.dt 'dt is the meme field
                tbl.close()
                ntest = strtran(tst,"{\"row",crlf()+"{\"row")
                nntest = ""
                cnt = w_count(ntest,crlf())
                FOR i = 2 TO cnt
                	nntest = nntest+word(ntest,i,crlf())+crlf()
                NEXT i
                lines = w_count(nntest,crlf())
                ccnt = w_count(word(nntest,1,crlf()),",")
                a_tbl = table.open("order")
                FOR l = 1 TO lines
                	FOR i = 1 TO ccnt
                		IF left(word(word(nntest,l,crlf()),i,","),5) = quote("sku")
                			a_tbl.enter_begin()
                			a_tbl.Sku = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.enter_end(.t.)
                		END IF
                	NEXT i
                	FOR i = 1 TO ccnt
                		IF left(word(word(nntest,l,crlf()),i,","),6) = quote("size")
                			a_tbl.change_begin()
                			a_tbl.Size = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)
                		else if substr(word(word(nntest,l,crlf()),i,","),2,7) = quote("rowid")
                			a_tbl.change_begin()
                			a_tbl.Rowid = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)
                		else if left(word(word(nntest,l,crlf()),i,","),4) = quote("id")
                			a_tbl.change_begin()
                			a_tbl.Id = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)
                		else if left(word(word(nntest,l,crlf()),i,","),5) = quote("qty")
                			a_tbl.change_begin()
                			a_tbl.Qty = word(word(word(nntest,l,crlf()),i,","),2,":")
                			a_tbl.change_end(.t.)
                		else if left(word(word(nntest,l,crlf()),i,","),7) = quote("price")
                			a_tbl.change_begin()
                			a_tbl.Price = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)
                		else if left(word(word(nntest,l,crlf()),i,","),6) = quote("name")
                			a_tbl.change_begin()
                			a_tbl.Name = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)	
                		else if left(word(word(nntest,l,crlf()),i,","),10) = quote("subtotal")
                			a_tbl.change_begin()
                			a_tbl.Subtotal = unquote(word(word(word(nntest,l,crlf()),i,","),2,":"))
                			a_tbl.change_end(.t.)
                		else if	left(word(word(nntest,l,crlf()),i,","),9) = quote("options")
                			a_tbl.change_begin()
                			a_tbl.Color = unquote(word(word(word(nntest,l,crlf()),i,","),3,":"))
                			a_tbl.change_end(.t.)
                		END IF
                	NEXT i
                NEXT l
                a_tbl.close()
                Assuming all character type fields in the order table.
                There can be only one.

                Comment


                  #9
                  Re: Parse JSON formatted field to create multiple records

                  If you know that all fields will be represented, this would be much quicker.

                  Code:
                  tbl = table.open("dtbl") 'the table holding the data
                  tst = tbl.dt 'dt is the meme field
                  tbl.close()
                  ntest = strtran(tst,"{\"row",crlf()+"{\"row")
                  nntest = ""
                  cnt = w_count(ntest,crlf())
                  FOR i = 2 TO cnt
                  	nntest = nntest+word(ntest,i,crlf())+crlf()
                  NEXT i
                  lines = w_count(nntest,crlf())
                  ccnt = w_count(word(nntest,1,crlf()),",")
                  a_tbl = table.open("order")
                  FOR l = 1 TO lines
                  	a_tbl.enter_begin()
                  	a_tbl.Sku = unquote(word(word(word(nntest,l,crlf()),8,","),2,":"))
                  	a_tbl.enter_end(.t.)
                  	a_tbl.change_begin()
                  	a_tbl.Size = unquote(word(word(word(nntest,l,crlf()),7,","),2,":"))
                  	a_tbl.Rowid = unquote(word(word(word(nntest,l,crlf()),1,","),2,":"))
                  	a_tbl.Id = unquote(word(word(word(nntest,l,crlf()),2,","),2,":"))
                  	a_tbl.Qty = word(word(word(nntest,l,crlf()),3,","),2,":")
                  	a_tbl.Price = unquote(word(word(word(nntest,l,crlf()),4,","),2,":"))
                  	a_tbl.Name = unquote(word(word(word(nntest,l,crlf()),5,","),2,":"))
                  	a_tbl.Subtotal = unquote(word(word(word(nntest,l,crlf()),9,","),2,":"))
                  	a_tbl.Color = unquote(word(word(word(nntest,l,crlf()),6,","),3,":"))
                  	a_tbl.change_end(.t.)
                  NEXT l
                  a_tbl.close()
                  There can be only one.

                  Comment


                    #10
                    Re: Parse JSON formatted field to create multiple records

                    IT WORKED !!!! PROBLEM SOLVED !!!

                    thanks Stan ! you rock. Some very advanced stuff with word() and handling options in this data set
                    Logical Design
                    Keven Thibeault
                    Boston, Massachusetts
                    Dwell in possibility

                    Comment


                      #11
                      Re: Parse JSON formatted field to create multiple records

                      I don't know how advanced it really is. You just have to recognize the patterns in the data structure and work out how to separate the whole into pieces that have the actual data you need.

                      Tedious to construct at times but Alpha has the tools you need.
                      There can be only one.

                      Comment

                      Working...
                      X