sql.Rows to JSON

446 views
Skip to first unread message

marc...@gmail.com

unread,
Feb 21, 2014, 3:16:43 PM2/21/14
to golan...@googlegroups.com

Hello,

I was hoping someone could share some code samples or point me to a library that would help me transform a sql.Rows (i.e. sql record set) into a JSON message.  I am dealing with several legacy database structure, so I can’t change some of the queries. I need to be able to transform sql.Rows to the JSON format provided below. Thank you very much in advance for your help!

Database structure layout, order by User Id. 

User Id, Name, Street, City, Zip, State, Email, Phone, Friend ID, Friend Name.

 

Records

0,"Peck Duran","Chapel Street","Orbin",”94999","Maine","pp...@bbb.com","+1 (859) 111-1111",0,"Hendricks Vaughan".

 

0,"Peck Duran","Chapel Street","Orbin",”94999","Maine","pp...@bbb.com","+1 (859) 111-1111",10," Yesenia Franks ".

 

0,"Peck Duran","Chapel Street","Orbin",”94999","Maine","pp...@bbb.com","+1 (859) 111-1111",13," Wilder Landry ".

 

2," Vilma Barrera","Bayview Place","Fleetmix",”94998","Washington","vvvvv@fff.com","+1 (884) 222-2222",3," Tisha Kline".

 

2," Vilma Barrera","Bayview Place","Fleetmix",”94998","Washington","vvvvv@fff.com","+1 (884) 222-2222",2," Mccarthy Moreno".

 

2," Vilma Barrera","Bayview Place","Fleetmix",”94998","Washington","vvvvv@fff.com","+1 (884) 222-2222",4," Neva Pennington".

 

You will notice the user record repeats as many time as it contains different friends.

 

Here is the JSON message format needed.

 

[

    {

        "id": 0,

        "name": "Peck Duran",

        "street": "Chapel Street",

        "city": "Orbin",

        "zip": "94999",

        "state": "Maine",

        "email": "pp...@bbb.com",

        "phone": "+1 (859) 111-1111",

        "friends": [

            {

                "id": 0,

                "name": "Hendricks Vaughan"

            },

            {

                "id": 10,

                "name": "Yesenia Franks"

            },

            {

                "id": 13,

                "name": "Wilder Landry"

            }

        ]

    },

    {

        "id": 1,

        "name": "Vilma Barrera",

        "street": "Bayview Place",

        "city": "Fleetmix",

        "zip": "94999",

        "state": "Washington",

        "email": "vvvvv@fff.com",

        "phone": "+1 (884) 222-2222",

        "friends": [

            {

                "id": 3,

                "name": "Tisha Kline"

            },

            {

                "id": 2,

                "name": "Mccarthy Moreno"

            },

            {

                "id": 4,

                "name": "Neva Pennington"

            }

        ]

    }]

Wes Freeman

unread,
Feb 23, 2014, 12:17:28 AM2/23/14
to marc...@gmail.com, golang-nuts
I just threw a quick and dirty example together with lib/pq... https://gist.github.com/wfreeman/9167240

I just iterate over the rows and decide whether to add a new friend or add a new user (I called it message, not really sure why).

Wes


Marcio Dasilva

unread,
Feb 23, 2014, 12:52:53 AM2/23/14
to golan...@googlegroups.com
Thank you very much, Wes!

Cheers to you,
Marcio
Reply all
Reply to author
Forward
0 new messages