[go-nuts] Re: JSON parser

2016-11-13 Thread Sergio Hunter
I did my code, thanks all who help me. Next task, It is necessary then json 
string pulls out three parameters, counted and discharged into a CSV format 
with the columns Date, Revenue, Gold. 


package main

import (
"database/sql"
"log"
_"github.com/go-sql-driver/mysql"
"compress/zlib"
"os"
"fmt"
"bytes"
"io"
"encoding/json"
)



func main() {
db, err := sql.Open("mysql", "name:password@tcp(127.0.0.1:port)/database")
if err != nil {
panic(err.Error())
}
defer db.Close()

rows, err := db.Query(`SELECT data FROM user_stats ORDER BY created_at 
LIMIT 10`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()

for rows.Next() {
var data []byte
err := rows.Scan()
if err != nil {
log.Fatal(err)
}
type UserStatsData struct {
Date string `json:"d"`
Revenue float64 `json:"r"`
Gold int `json:"g"`
}
userStatsData := UserStatsData{}
err = json.Unmarshal(data, )
if err != nil {
r, err := zlib.NewReader(bytes.NewReader(data))
if err != nil {
log.Panicf("Cannot read archive %v", err);
}
io.Copy(os.Stdout, r)
r.Close()
}

}
}


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-11 Thread Sergio Hunter
Thanks so much all who help me, I appreciate it!

Ok, for example: 
first row achieve:
xÚ –Ûjã0 E ¥èÙ.:GWûWJ   2 e ¦} ù÷êb7Ú;  
yñòÖ>  )º˜×Ÿoï?^ÿ ÿü5«Q+a  ­{²yUY­>Í6[k&ónÖ‹9ŸÍ*b­ Œ¶çòX þ›Õ?/Ëu Éò¼äB~›õåRŸm{ 
êÛ_=˜Ì¶ü¼¹N—ÏW]x „Ym 

[go-nuts] Re: JSON parser

2016-11-10 Thread Tamás Gulácsi
2016. november 10., csütörtök 21:20:46 UTC+1 időpontban Sergio Hunter a 
következőt írta:
>
> I can't because аccess to the database is only granted to me.
> If leave my code and only add code which would be skip text rows and 
> continues to unpack the archive?
>
>
We haven't asked for access to the db, just some sample code!

If you want to distinguish between (probably) JSON and other text, that's 
easy: JSON starts with '{'.
If you want to distinguish between gzipped and non-gzipped text, that's 
easy: gzip starts with "\x1f\x8b": http://www.ietf.org/rfc/rfc1952.txt
T

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread Sergio Hunter
I can't because аccess to the database is only granted to me.
If leave my code and only add code which would be skip text rows and 
continues to unpack the archive?


четверг, 10 ноября 2016 г., 19:34:33 UTC+2 пользователь pierre...@gmail.com 
написал:
>
> Then you need to provide sample data...
>
> Le jeudi 10 novembre 2016 18:33:10 UTC+1, Sergio Hunter a écrit :
>>
>> I agree with you) But what do i do with this, It's exactly the same error.
>>
>> четверг, 10 ноября 2016 г., 19:05:12 UTC+2 пользователь 
>> pierre...@gmail.com написал:
>>>
>>> We will get there but is is hard hitting your target when shooting in 
>>> the dark :).
>>>
>>> Remove white spaces:
>>> https://play.golang.org/p/_LktUPDhYW
>>>
>>> Le jeudi 10 novembre 2016 17:52:21 UTC+1, Sergio Hunter a écrit :

 Thanks for your help. 
 Do you know what it means? 
 2016/11/10 18:46:21 invalid character '\u0085' looking for beginning of 
 value
 exit status 1


 четверг, 10 ноября 2016 г., 18:32:40 UTC+2 пользователь 
 pierre...@gmail.com написал:
>
> Sorry, I did not test it. This one passes the playground build (up to 
> the mysql driver not being found):
> https://play.golang.org/p/_WfTMOd-s6
>
> Le jeudi 10 novembre 2016 17:13:19 UTC+1, Sergio Hunter a écrit :
>>
>> The compiler generates an error
>>
>> Test.go:71: undefined: o
>> Test.go:85: cannot use nil as type Object in return argument
>>
>>
>>
>>
>>
>> четверг, 10 ноября 2016 г., 17:56:58 UTC+2 пользователь 
>> pierre...@gmail.com написал:
>>>
>>> Ok, then something along those lines:
>>> https://play.golang.org/p/dMPQIrEdNN
>>>
>>> Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :

 Thanks so much for your answer. 
 I'll try to explain what I need. I have a field "data",inside field 
 has compress rows and text data(json), I need decompress data, but my 
 code 
 begins to unpack the file to text and compiler generates an error.
 The logic is this: It reads "data" of the database, do 
 "json.NewDecoder", if "json.NewDecoder" did not work - will "err", 
 then if 
 there are "err" is necessary to decompress and do again 
 "json.NewDecoder". 
 I hope you are understand me, thanks.

 четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon 
 Ritchie написал:
>
> I'm also not sure what you are trying to do.  However, I think I 
> can see a bug in your code.
>
> First you run a database query to get a list of values from a 
> field called data.  This could produce up to 10 values.
>
> Next you have a loop which runs through the values and discards 
> them.  At the end of the loop, you have the data field from the last 
> record, and you have thrown away all the others.  Is this what you 
> want?
>
> Finally, you unzip the last data field.
>
> So if your table contains 100 records, you will get 10 of them, 
> throw away the first 9 records and unzip the data from the last one.
>
> I hope this helps.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread pierre . curto
Then you need to provide sample data...

Le jeudi 10 novembre 2016 18:33:10 UTC+1, Sergio Hunter a écrit :
>
> I agree with you) But what do i do with this, It's exactly the same error.
>
> четверг, 10 ноября 2016 г., 19:05:12 UTC+2 пользователь 
> pierre...@gmail.com написал:
>>
>> We will get there but is is hard hitting your target when shooting in the 
>> dark :).
>>
>> Remove white spaces:
>> https://play.golang.org/p/_LktUPDhYW
>>
>> Le jeudi 10 novembre 2016 17:52:21 UTC+1, Sergio Hunter a écrit :
>>>
>>> Thanks for your help. 
>>> Do you know what it means? 
>>> 2016/11/10 18:46:21 invalid character '\u0085' looking for beginning of 
>>> value
>>> exit status 1
>>>
>>>
>>> четверг, 10 ноября 2016 г., 18:32:40 UTC+2 пользователь 
>>> pierre...@gmail.com написал:

 Sorry, I did not test it. This one passes the playground build (up to 
 the mysql driver not being found):
 https://play.golang.org/p/_WfTMOd-s6

 Le jeudi 10 novembre 2016 17:13:19 UTC+1, Sergio Hunter a écrit :
>
> The compiler generates an error
>
> Test.go:71: undefined: o
> Test.go:85: cannot use nil as type Object in return argument
>
>
>
>
>
> четверг, 10 ноября 2016 г., 17:56:58 UTC+2 пользователь 
> pierre...@gmail.com написал:
>>
>> Ok, then something along those lines:
>> https://play.golang.org/p/dMPQIrEdNN
>>
>> Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :
>>>
>>> Thanks so much for your answer. 
>>> I'll try to explain what I need. I have a field "data",inside field 
>>> has compress rows and text data(json), I need decompress data, but my 
>>> code 
>>> begins to unpack the file to text and compiler generates an error.
>>> The logic is this: It reads "data" of the database, do 
>>> "json.NewDecoder", if "json.NewDecoder" did not work - will "err", then 
>>> if 
>>> there are "err" is necessary to decompress and do again 
>>> "json.NewDecoder". 
>>> I hope you are understand me, thanks.
>>>
>>> четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon 
>>> Ritchie написал:

 I'm also not sure what you are trying to do.  However, I think I 
 can see a bug in your code.

 First you run a database query to get a list of values from a field 
 called data.  This could produce up to 10 values.

 Next you have a loop which runs through the values and discards 
 them.  At the end of the loop, you have the data field from the last 
 record, and you have thrown away all the others.  Is this what you 
 want?

 Finally, you unzip the last data field.

 So if your table contains 100 records, you will get 10 of them, 
 throw away the first 9 records and unzip the data from the last one.

 I hope this helps.



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread Sergio Hunter
I agree with you) But what do i do with this, It's exactly the same error.

четверг, 10 ноября 2016 г., 19:05:12 UTC+2 пользователь pierre...@gmail.com 
написал:
>
> We will get there but is is hard hitting your target when shooting in the 
> dark :).
>
> Remove white spaces:
> https://play.golang.org/p/_LktUPDhYW
>
> Le jeudi 10 novembre 2016 17:52:21 UTC+1, Sergio Hunter a écrit :
>>
>> Thanks for your help. 
>> Do you know what it means? 
>> 2016/11/10 18:46:21 invalid character '\u0085' looking for beginning of 
>> value
>> exit status 1
>>
>>
>> четверг, 10 ноября 2016 г., 18:32:40 UTC+2 пользователь 
>> pierre...@gmail.com написал:
>>>
>>> Sorry, I did not test it. This one passes the playground build (up to 
>>> the mysql driver not being found):
>>> https://play.golang.org/p/_WfTMOd-s6
>>>
>>> Le jeudi 10 novembre 2016 17:13:19 UTC+1, Sergio Hunter a écrit :

 The compiler generates an error

 Test.go:71: undefined: o
 Test.go:85: cannot use nil as type Object in return argument





 четверг, 10 ноября 2016 г., 17:56:58 UTC+2 пользователь 
 pierre...@gmail.com написал:
>
> Ok, then something along those lines:
> https://play.golang.org/p/dMPQIrEdNN
>
> Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :
>>
>> Thanks so much for your answer. 
>> I'll try to explain what I need. I have a field "data",inside field 
>> has compress rows and text data(json), I need decompress data, but my 
>> code 
>> begins to unpack the file to text and compiler generates an error.
>> The logic is this: It reads "data" of the database, do 
>> "json.NewDecoder", if "json.NewDecoder" did not work - will "err", then 
>> if 
>> there are "err" is necessary to decompress and do again 
>> "json.NewDecoder". 
>> I hope you are understand me, thanks.
>>
>> четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon Ritchie 
>> написал:
>>>
>>> I'm also not sure what you are trying to do.  However, I think I can 
>>> see a bug in your code.
>>>
>>> First you run a database query to get a list of values from a field 
>>> called data.  This could produce up to 10 values.
>>>
>>> Next you have a loop which runs through the values and discards 
>>> them.  At the end of the loop, you have the data field from the last 
>>> record, and you have thrown away all the others.  Is this what you want?
>>>
>>> Finally, you unzip the last data field.
>>>
>>> So if your table contains 100 records, you will get 10 of them, 
>>> throw away the first 9 records and unzip the data from the last one.
>>>
>>> I hope this helps.
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread Sergio Hunter
Thanks for your help. 
Do you know what it means? 
2016/11/10 18:46:21 invalid character '\u0085' looking for beginning of 
value
exit status 1


четверг, 10 ноября 2016 г., 18:32:40 UTC+2 пользователь pierre...@gmail.com 
написал:
>
> Sorry, I did not test it. This one passes the playground build (up to the 
> mysql driver not being found):
> https://play.golang.org/p/_WfTMOd-s6
>
> Le jeudi 10 novembre 2016 17:13:19 UTC+1, Sergio Hunter a écrit :
>>
>> The compiler generates an error
>>
>> Test.go:71: undefined: o
>> Test.go:85: cannot use nil as type Object in return argument
>>
>>
>>
>>
>>
>> четверг, 10 ноября 2016 г., 17:56:58 UTC+2 пользователь 
>> pierre...@gmail.com написал:
>>>
>>> Ok, then something along those lines:
>>> https://play.golang.org/p/dMPQIrEdNN
>>>
>>> Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :

 Thanks so much for your answer. 
 I'll try to explain what I need. I have a field "data",inside field has 
 compress rows and text data(json), I need decompress data, but my code 
 begins to unpack the file to text and compiler generates an error.
 The logic is this: It reads "data" of the database, do 
 "json.NewDecoder", if "json.NewDecoder" did not work - will "err", then if 
 there are "err" is necessary to decompress and do again "json.NewDecoder". 
 I hope you are understand me, thanks.

 четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon Ritchie 
 написал:
>
> I'm also not sure what you are trying to do.  However, I think I can 
> see a bug in your code.
>
> First you run a database query to get a list of values from a field 
> called data.  This could produce up to 10 values.
>
> Next you have a loop which runs through the values and discards them. 
>  At the end of the loop, you have the data field from the last record, 
> and 
> you have thrown away all the others.  Is this what you want?
>
> Finally, you unzip the last data field.
>
> So if your table contains 100 records, you will get 10 of them, throw 
> away the first 9 records and unzip the data from the last one.
>
> I hope this helps.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread pierre . curto
Sorry, I did not test it. This one passes the playground build (up to the 
mysql driver not being found):
https://play.golang.org/p/_WfTMOd-s6

Le jeudi 10 novembre 2016 17:13:19 UTC+1, Sergio Hunter a écrit :
>
> The compiler generates an error
>
> Test.go:71: undefined: o
> Test.go:85: cannot use nil as type Object in return argument
>
>
>
>
>
> четверг, 10 ноября 2016 г., 17:56:58 UTC+2 пользователь 
> pierre...@gmail.com написал:
>>
>> Ok, then something along those lines:
>> https://play.golang.org/p/dMPQIrEdNN
>>
>> Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :
>>>
>>> Thanks so much for your answer. 
>>> I'll try to explain what I need. I have a field "data",inside field has 
>>> compress rows and text data(json), I need decompress data, but my code 
>>> begins to unpack the file to text and compiler generates an error.
>>> The logic is this: It reads "data" of the database, do 
>>> "json.NewDecoder", if "json.NewDecoder" did not work - will "err", then if 
>>> there are "err" is necessary to decompress and do again "json.NewDecoder". 
>>> I hope you are understand me, thanks.
>>>
>>> четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon Ritchie 
>>> написал:

 I'm also not sure what you are trying to do.  However, I think I can 
 see a bug in your code.

 First you run a database query to get a list of values from a field 
 called data.  This could produce up to 10 values.

 Next you have a loop which runs through the values and discards them. 
  At the end of the loop, you have the data field from the last record, and 
 you have thrown away all the others.  Is this what you want?

 Finally, you unzip the last data field.

 So if your table contains 100 records, you will get 10 of them, throw 
 away the first 9 records and unzip the data from the last one.

 I hope this helps.



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread pierre . curto
Ok, then something along those lines:
https://play.golang.org/p/dMPQIrEdNN

Le jeudi 10 novembre 2016 16:36:21 UTC+1, Sergio Hunter a écrit :
>
> Thanks so much for your answer. 
> I'll try to explain what I need. I have a field "data",inside field has 
> compress rows and text data(json), I need decompress data, but my code 
> begins to unpack the file to text and compiler generates an error.
> The logic is this: It reads "data" of the database, do "json.NewDecoder", 
> if "json.NewDecoder" did not work - will "err", then if there are "err" is 
> necessary to decompress and do again "json.NewDecoder". 
> I hope you are understand me, thanks.
>
> четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon Ritchie 
> написал:
>>
>> I'm also not sure what you are trying to do.  However, I think I can see 
>> a bug in your code.
>>
>> First you run a database query to get a list of values from a field 
>> called data.  This could produce up to 10 values.
>>
>> Next you have a loop which runs through the values and discards them.  At 
>> the end of the loop, you have the data field from the last record, and you 
>> have thrown away all the others.  Is this what you want?
>>
>> Finally, you unzip the last data field.
>>
>> So if your table contains 100 records, you will get 10 of them, throw 
>> away the first 9 records and unzip the data from the last one.
>>
>> I hope this helps.
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-10 Thread Sergio Hunter
Thanks so much for your answer. 
I'll try to explain what I need. I have a field "data",inside field has 
compress rows and text data(json), I need decompress data, but my code 
begins to unpack the file to text and compiler generates an error.
The logic is this: It reads "data" of the database, do "json.NewDecoder", 
if "json.NewDecoder" did not work - will "err", then if there are "err" is 
necessary to decompress and do again "json.NewDecoder". 
I hope you are understand me, thanks.

четверг, 10 ноября 2016 г., 13:03:28 UTC+2 пользователь Simon Ritchie 
написал:
>
> I'm also not sure what you are trying to do.  However, I think I can see a 
> bug in your code.
>
> First you run a database query to get a list of values from a field called 
> data.  This could produce up to 10 values.
>
> Next you have a loop which runs through the values and discards them.  At 
> the end of the loop, you have the data field from the last record, and you 
> have thrown away all the others.  Is this what you want?
>
> Finally, you unzip the last data field.
>
> So if your table contains 100 records, you will get 10 of them, throw away 
> the first 9 records and unzip the data from the last one.
>
> I hope this helps.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: JSON parser

2016-11-09 Thread pierre . curto
Hello,

I am sorry I am not sure I understood what you wanted to achieve, however, 
if you need to extract a json object from your sql rows, here is some code 
that should do it:
https://play.golang.org/p/92oFkprZfs

HTH

Pierre

Le mercredi 9 novembre 2016 22:36:42 UTC+1, Sergio Hunter a écrit :
>
> Please tell me how to properly finish the JSON parser, so when the line 
> did not fall unzip the code? That is, when you compile the code meets the 
> text rather than the archive, immediately falls. It is necessary to write 
> to skip the line with the text. When I start the first line unpacks it, and 
> then when it sees the text drops, so it is necessary to write to pass and 
> go on to unpack the archive. Sorry for my English. I hope you are 
> understood me)
>
> package main   import ( "database/sql" "log" _"
> github.com/go-sql-driver/mysql" "compress/zlib" "bytes" "os" "fmt" 
> "encoding/json" )   func main() { db, err := sql.Open("mysql", 
> "name:password@tcp(127.0.0.1:port)/database") if err != nil { panic(err.
> Error()) } defer db.Close()   rows, err := db.Query(`SELECT data FROM 
> user_stats ORDER BY created_at LIMIT 10`) if err != nil { log.Fatal(err) } 
> defer rows.Close()   var data []byte   for rows.Next() { err := rows.Scan(
> ) if err != nil { log.Fatal(err) }   r, err := zlib.NewReader(bytes.
> NewReader(data)) if err != nil { log.Panicf("Cannot read archive %v", err)
> ; } io.Copy(os.Stdout, r) r.Close() } }
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.