[Proto-Scripty] Re: CSV to Json
Eric - that is smoother, I'll update my scripts to run it that way thanks! On Aug 22, 5:01 am, Eric wrote: > Jason, > > Why do you copy the return array into an other one before using it? > (and why rewriting code already available as native PHP functions?) > > This should do the same than your code: > $fh = fopen($filename); > $fields = fgetcsv($fh); > while($line = fgetcsv($fh) !== false) > { > print json_encode(array_combine($fields,$line));} > > fclose($fh); > > Eric > > PS: You may want to add some error handling :o) > > On Aug 20, 6:03 pm, Jason wrote: > > > > > > > > > I would use a small PHP script (assuming the first line is the field > > names) > > > $fields = array(); > > $data = array(); > > $fh = fopen($filename); > > $line = fgetcsv($fh); > > foreach($line as $n) > > { > > $fields[] = $n; > > > } > > > while($line = fgetcsv($fh) !== false) > > { > > foreach($line as $key => $n) > > { > > $data[$fields[$key]][] = $n; > > > } > > } > > > print json_encode($data); > > > seehttp://us.php.net/fgetcsvformore examples > > > On Aug 18, 11:24 pm, kstubs wrote: > > > > Any tips for converting CSV to Json? I have CSV, row deliminted \n, and > > > fields delimited with typical comma. There is a known/exisiting object > > > type > > > for each field. Something like: > > > > {'fields': [{'field':'fname', 'col':2}, {'field':'ssn', 'col':5}]} > > > > So I have that to work from, and will populate a Json result like this: > > > [{'data':{'fname':value}, {'ssn':value}},{'data':{'fname':value}, > > > {'ssn':value}}] > > > > I feel like I am doing it the long way when I: > > > > // split csv string by \n to new line array > > > > // for every item in new line split string > > > > // split item by , to field array > > > > // for every item in field array > > > > // create object to add to new data array > > > > Any ideas would help. > > > > Thanks, > > > Karl.. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: CSV to Json
Jason, Why do you copy the return array into an other one before using it? (and why rewriting code already available as native PHP functions?) This should do the same than your code: $fh = fopen($filename); $fields = fgetcsv($fh); while($line = fgetcsv($fh) !== false) { print json_encode(array_combine($fields,$line)); } fclose($fh); Eric PS: You may want to add some error handling :o) On Aug 20, 6:03 pm, Jason wrote: > I would use a small PHP script (assuming the first line is the field > names) > > $fields = array(); > $data = array(); > $fh = fopen($filename); > $line = fgetcsv($fh); > foreach($line as $n) > { > $fields[] = $n; > > } > > while($line = fgetcsv($fh) !== false) > { > foreach($line as $key => $n) > { > $data[$fields[$key]][] = $n; > > } > } > > print json_encode($data); > > seehttp://us.php.net/fgetcsvfor more examples > > On Aug 18, 11:24 pm, kstubs wrote: > > > > > > > > > Any tips for converting CSV to Json? I have CSV, row deliminted \n, and > > fields delimited with typical comma. There is a known/exisiting object type > > for each field. Something like: > > > {'fields': [{'field':'fname', 'col':2}, {'field':'ssn', 'col':5}]} > > > So I have that to work from, and will populate a Json result like this: > > [{'data':{'fname':value}, {'ssn':value}},{'data':{'fname':value}, > > {'ssn':value}}] > > > I feel like I am doing it the long way when I: > > > // split csv string by \n to new line array > > > // for every item in new line split string > > > // split item by , to field array > > > // for every item in field array > > > // create object to add to new data array > > > Any ideas would help. > > > Thanks, > > Karl.. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: CSV to Json
You can invert your {'fields': [{'field':'fname', 'col':2}, {'field':'ssn', 'col':5}]} to something like ['field0', 'field1', 'fname', 'field3', 'field4', 'ssn'] and then zip it (Enumerable#zip) with every CSV row. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/TW9sKYDkBvQJ. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: CSV to Json
Client side only.. I have something written but not sure I'm taking advantage of prototype added methods for arrays and enumerations. Basically, I'm constructing a csv from json object iterating over it's data array, and appending to CSV like: csv+=data csv+=',' csv+='\n' The above is *obviously* chopped significantly and missing the smarts around when to add , and when to add \n.. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/l9Z5qbY3CXwJ. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: CSV to Json
I would use a small PHP script (assuming the first line is the field names) $fields = array(); $data = array(); $fh = fopen($filename); $line = fgetcsv($fh); foreach($line as $n) { $fields[] = $n; } while($line = fgetcsv($fh) !== false) { foreach($line as $key => $n) { $data[$fields[$key]][] = $n; } } print json_encode($data); see http://us.php.net/fgetcsv for more examples On Aug 18, 11:24 pm, kstubs wrote: > Any tips for converting CSV to Json? I have CSV, row deliminted \n, and > fields delimited with typical comma. There is a known/exisiting object type > for each field. Something like: > > {'fields': [{'field':'fname', 'col':2}, {'field':'ssn', 'col':5}]} > > So I have that to work from, and will populate a Json result like this: > [{'data':{'fname':value}, {'ssn':value}},{'data':{'fname':value}, > {'ssn':value}}] > > I feel like I am doing it the long way when I: > > // split csv string by \n to new line array > > // for every item in new line split string > > // split item by , to field array > > // for every item in field array > > // create object to add to new data array > > Any ideas would help. > > Thanks, > Karl.. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.