Skip to content

How to use YAML, XML, CSV?

Saddam H edited this page Mar 11, 2020 · 6 revisions

By default gojsonq use JSON data for query, you may use YAML, XML, CSV data too.

YAML

You can use YAML data to query using this package as it's a strict superset of JSON. The SetDecoder option can be used to obtain this.

Sample data (data.yaml). [Data source: https://yaml.org/start.html]

invoice: 34843 date: 2001-01-23T00:00:00.000Z bill-to: given: Chris family: Dumars address: lines: "458 Walkman Dr.\nSuite #292\n" city: 'Royal Oak' state: MI postal: 48046 ship-to: given: Chris family: Dumars address: lines: "458 Walkman Dr.\nSuite #292\n" city: 'Royal Oak' state: MI postal: 48046 product: - sku: BL394D quantity: 4 description: Basketball price: 450 - sku: BL4438H quantity: 1 description: 'Super Hoop' price: 2392 tax: 251.42 total: 4443.52 comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n"

Lets see an example:

package main

import (
	"encoding/json"
	"fmt"

	y "github.com/ghodss/yaml"
	gojsonq "github.com/thedevsaddam/gojsonq/v2"
)

func main() {
	jq := gojsonq.New(gojsonq.SetDecoder(&yamlDecoder{})).File("./data.yaml")
	jq.From("product").Where("price", "<=", 500)
	fmt.Printf("%v\n", jq.First())
}

type yamlDecoder struct {
}

func (i *yamlDecoder) Decode(data []byte, v interface{}) error {
	bb, err := y.YAMLToJSON(data)
	if err != nil {
		return err
	}
	return json.Unmarshal(bb, &v)
}

XML

The SetDecoder option can be used to query XML data like YAML.

Sample data (data.xml). [Data source: https://www.w3schools.com/xml/]

<breakfast_menu> Belgian Waffles $5.95 Two of our famous Belgian Waffles with plenty of real maple syrup 650 Strawberry Belgian Waffles $7.95 Light Belgian waffles covered with strawberries and whipped cream 900 Berry-Berry Belgian Waffles $8.95 Belgian waffles covered with assorted fresh berries and whipped cream 900 French Toast $4.50 Thick slices made from our homemade sourdough bread 600 Homestyle Breakfast $6.95 Two eggs, bacon or sausage, toast, and our ever-popular hash browns 950 </breakfast_menu>

See the example below:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	xj "github.com/basgys/goxml2json"
	gojsonq "github.com/thedevsaddam/gojsonq/v2"
)

func main() {
	jq := gojsonq.New(gojsonq.SetDecoder(&xmlDecoder{})).File("./data.xml")
	jq.From("breakfast_menu.food").Where("calories", "=", "600")
	fmt.Printf("%v\n", jq.Get())
}

type xmlDecoder struct {
}

func (i *xmlDecoder) Decode(data []byte, v interface{}) error {
	buf, err := xj.Convert(bytes.NewReader(data))
	if err != nil {
		return err
	}
	return json.Unmarshal(buf.Bytes(), &v)
}

CSV

The SetDecoder option can be used to query CSV data like YAML, XML.

Sample data (data.csv). [Data source: https://www.theguardian.com/news/datablog/2011/jan/01/top-100-books-of-all-time#data]

Index,Position,ISBN,Title,Author,Imprint,Publisher Group,Volume,Value,RRP,ASP,Binding,Publ Date,Product Class 1,1,9780552149518,"Da Vinci Code,The","Brown, Dan",Corgi Books,Transworld Grp,"4,522,025","£22,857,837.53",£7.99,£5.05,Paperback,1 Mar 2004,"F2.1 Crime, Thriller & Adventure" 3,2,9780747532743,Harry Potter and the Philosopher's Stone#,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"3,844,316","£19,853,187.43",£6.99,£5.22,Paperback,26 Jun 1997,Y2.1 Children's Fiction 7,3,9780747538486,Harry Potter and the Chamber of Secrets#,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"3,184,492","£16,224,021.98",£6.99,£5.07,Paperback,1 Apr 1999,Y2.1 Children's Fiction 6,4,9780552150736,Angels and Demons#,"Brown, Dan",Corgi Books,Transworld Grp,"3,096,850","£15,537,324.84",£7.99,£5.05,Paperback,1 Jul 2003,"F2.1 Crime, Thriller & Adventure" 2,5,9780747551003,Harry Potter and the Order of the Phoenix,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"3,043,226","£33,925,431.19",£16.99,£11.15,Hardback,21 Jun 2003,Y2.1 Children's Fiction 4,6,9780747581086,Harry Potter and the Half-blood Prince:Children's Edition,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"2,947,565","£28,030,020.14",£16.99,£9.51,Hardback,16 Jul 2005,Y2.1 Children's Fiction 5,7,9780747591054,Harry Potter and the Deathly Hallows,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"2,842,059","£25,028,396.30",£17.99,£8.81,Hardback,21 Jul 2007,Y2.1 Children's Fiction 8,8,9780747546290,Harry Potter and the Prisoner of Azkaban#,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"2,776,314","£14,140,490.93",£6.99,£5.05,Paperback,1 Apr 2000,Y2.1 Children's Fiction 15,9,9781904233657,Twilight#,"Meyer, Stephenie",ATOM,"Little, Brown Book Grp","2,105,862","£10,148,762.73",£7.99,£4.80,Paperback,22 Mar 2007,Y2.2 Young Adult Fiction 9,10,9780747550990,Harry Potter and the Goblet of Fire,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"2,057,397","£10,745,711.77",£8.99,£5.22,Paperback,6 Jul 2001,Y2.1 Children's Fiction 10,11,9780552151764,Deception Point#,"Brown, Dan",Corgi Books,Transworld Grp,"2,018,264","£10,047,195.89",£7.99,£4.95,Paperback,1 May 2004,"F2.1 Crime, Thriller & Adventure" 14,12,9781904233886,New Moon#,"Meyer, Stephenie",ATOM,"Little, Brown Book Grp","1,975,659","£9,425,640.57",£7.99,£4.83,Paperback,6 Sep 2007,Y2.2 Young Adult Fiction 667,13,9780330457729,"Lovely Bones,The#","Sebold, Alice",Picador,Pan Macmillan Grp,"1,942,042","£11,561,201.71",£7.99,£5.24,Paperback,4 Dec 2009,F1.1 General & Literary Fiction 11,14,9780552151696,Digital Fortress#,"Brown, Dan",Corgi Books,Transworld Grp,"1,911,943","£9,642,124.01",£7.99,£5.02,Paperback,5 Mar 2004,"F2.1 Crime, Thriller & Adventure" 16,15,9780099450252,"Curious Incident of the Dog in the Night-time,The#","Haddon, Mark",Vintage,Random House Grp,"1,868,969","£10,196,570.23",£7.99,£5.40,Paperback,1 Apr 2004,F1.1 General & Literary Fiction 12,16,9781904233916,Eclipse#,"Meyer, Stephenie",ATOM,"Little, Brown Book Grp","1,846,171","£8,981,704.14",£7.99,£4.86,Paperback,3 Jul 2008,Y2.2 Young Adult Fiction 24,17,9781847245458,"Girl with the Dragon Tattoo,The:Millennium Trilogy#","Larsson, Stieg",Quercus,Quercus Grp,"1,697,425","£9,205,553.55",£7.99,£5.51,Paperback,24 Jul 2008,"F2.1 Crime, Thriller & Adventure" 26,18,9780747566533,"Kite Runner,The#","Hosseini, Khaled",Bloomsbury Publishing PLC,Bloomsbury Grp,"1,551,953","£9,317,049.76",£7.99,£6.08,Paperback,7 Jun 2004,F1.1 General & Literary Fiction 17,19,9780099464464,"Time Traveler's Wife,The","Niffenegger, Audrey",Vintage,Random House Grp,"1,496,081","£8,227,127.81",£7.99,£5.50,Paperback,29 May 2004,F1.1 General & Literary Fiction 18,20,9780141017891,"World According to Clarkson,The","Clarkson, Jeremy",Penguin Books,Penguin Grp,"1,447,188","£7,503,190.76",£7.99,£5.18,Paperback,26 May 2005,T13.0 Humour: Collections & General 60,21,9780099429791,Atonement#,"McEwan, Ian",Vintage,Random House Grp,"1,396,366","£8,671,447.35",£7.99,£6.74,Paperback,2 May 2002,F1.1 General & Literary Fiction 19,22,9780593054277,"Lost Symbol,The","Brown, Dan",Bantam Press,Transworld Grp,"1,370,347","£13,052,345.93",£18.99,£9.52,Hardback,15 Sep 2009,"F2.1 Crime, Thriller & Adventure" 20,23,9780552997041,"Short History of Nearly Everything,A","Bryson, Bill",Black Swan,Transworld Grp,"1,334,635","£9,911,709.25",£9.99,£7.43,Paperback,1 Jun 2004,T8.0 Popular Science 21,24,9781905654284,Breaking Dawn,"Meyer, Stephenie",ATOM,"Little, Brown Book Grp","1,315,685","£12,034,126.42",£14.99,£9.15,Hardback,4 Aug 2008,Y2.2 Young Adult Fiction 22,25,9780747546245,Harry Potter and the Goblet of Fire,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"1,297,411","£14,875,830.76",£17.99,£11.47,Hardback,8 Jul 2000,Y2.1 Children's Fiction 23,26,9780747591061,Harry Potter and the Deathly Hallows,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"1,283,638","£12,081,283.05",£17.99,£9.41,Hardback,21 Jul 2007,F2.2 Science Fiction & Fantasy "1,400",27,9781849163422,"Girl Who Played With Fire,The:Millennium Trilogy#","Larsson, Stieg",Quercus Publishing Plc,Quercus Grp,"1,265,474","£6,706,614.76",£7.99,£5.18,Paperback,29 Jul 2010,"F2.1 Crime, Thriller & Adventure" 27,28,9780752837505,"Child Called It,A","Pelzer, Dave",Orion (an Imprint of The Orion,Orion Grp,"1,116,042","£5,566,648.20",£6.99,£4.99,Paperback,4 Jan 2001,T4.0A Autobiography: General 28,29,9780349116754,"No.1 Ladies' Detective Agency,The:No.1 Ladies' Detective Agency S.","McCall Smith, Alexander",Abacus,"Little, Brown Book Grp","1,106,219","£6,322,495.98",£7.99,£5.72,Paperback,5 Jun 2003,"F2.1 Crime, Thriller & Adventure" 29,30,9780718147655,You are What You Eat:The Plan That Will Change Your Life,"McKeith, Gillian",Michael Joseph,Penguin Grp,"1,103,619","£11,766,885.22",£14.99,£10.66,Paperback,17 Jun 2004,T9.4 Fitness & Diet 30,31,9780006512134,Man and Boy,"Parsons, Tony",HarperCollins Publishers,HarperCollins Grp,"1,100,447","£6,857,434.41",£7.99,£6.23,Paperback,6 Mar 2000,F1.1 General & Literary Fiction 32,32,9780099387916,Birdsong,"Faulks, Sebastian",Vintage,Random House Grp,"1,078,571","£7,207,433.66",£7.99,£6.68,Paperback,18 Jul 1994,F1.1 General & Literary Fiction 33,33,9780752877327,Labyrinth,"Mosse, Kate",Orion (an Imprint of The Orion,Orion Grp,"1,065,008","£5,860,821.19",£7.99,£5.50,Paperback,11 Jan 2006,F1.1 General & Literary Fiction 34,34,9780755309511,"Island,The","Hislop, Victoria",Headline Review,Headline Grp,"1,056,432","£5,680,549.73",£7.99,£5.38,Paperback,10 Apr 2006,F1.1 General & Literary Fiction 35,35,9781841953922,Life of Pi,"Martel, Yann",Canongate Books,Canongate Grp,"1,050,090","£6,967,370.73",£7.99,£6.64,Paperback,17 May 2003,F1.1 General & Literary Fiction 36,36,9780091889487,"Dr. Atkins' New Diet Revolution:The No-hunger, Luxurious Weight Loss P","Atkins, Robert C.",Vermilion,Random House Grp,"1,046,531","£7,274,727.01",£8.99,£6.95,Paperback,2 Jan 2003,T9.4 Fitness & Diet 37,37,9780747599876,"Tales of Beedle the Bard,The","Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"1,039,823","£4,185,291.09",£6.99,£4.03,Hardback,4 Dec 2008,Y2.1 Children's Fiction 38,38,9780749397548,Captain Corelli's Mandolin,"De Bernieres, Louis",Minerva,Random House Grp,"1,025,664","£7,026,297.50",£7.99,£6.85,Paperback,1 Jun 1995,F1.1 General & Literary Fiction 39,39,9780563384304,Delia's How to Cook:(Bk.1) ,"Smith, Delia",BBC Books (Random House),Random House Grp,"1,014,854","£11,420,572.33",£20.00,£11.25,Hardback,12 Oct 1998,T16.0 Food & Drink: General "4,281",40,9780330507417,"Gruffalo,The#","Donaldson, Julia",Macmillan Children's Books,Pan Macmillan Grp,"1,007,208","£4,770,356.81",£5.99,£4.39,Paperback,6 Mar 2009,Y1.3 Picture Books 40,41,9781861976123,"Eats, Shoots and Leaves:The Zero Tolerance Approach to Punctuation","Truss, Lynne",Profile Books,Profile Books Group,"995,455","£8,858,171.44",£9.99,£8.90,Hardback,6 Nov 2003,T2.1 Usage & Writing Guides 42,42,9780590660549,Northern Lights:His Dark Materials S.,"Pullman, Philip",Point,Scholastic Ltd. Grp,"976,435","£5,610,127.17",£6.99,£5.75,Paperback,23 Oct 1998,Y2.2 Young Adult Fiction 44,43,9780755331420,"Interpretation of Murder,The","Rubenfeld, Jed",Headline Review,Headline Grp,"950,686","£5,425,925.60",£7.99,£5.71,Paperback,15 Jan 2007,"F2.1 Crime, Thriller & Adventure" 45,44,9781849162746,"Girl Who Kicked the Hornets' Nest,The:Millennium Trilogy","Larsson, Stieg",Quercus Publishing Plc,Quercus Grp,"942,103","£4,841,500.15",£7.99,£5.14,Paperback,1 Apr 2010,"F2.1 Crime, Thriller & Adventure" 47,45,9780330367356,Bridget Jones: The Edge of Reason,"Fielding, Helen",Picador,Pan Macmillan Grp,"923,429","£5,495,835.32",£7.99,£5.95,Paperback,15 Jun 2000,F1.1 General & Literary Fiction 48,46,9780141020525,"Short History of Tractors in Ukrainian,A","Lewycka, Marina",Penguin Books,Penguin Grp,"904,498","£5,710,250.78",£7.99,£6.31,Paperback,2 Mar 2006,F1.1 General & Literary Fiction 49,47,9780722532935,"Alchemist,The:A Fable About Following Your Dream","Coelho, Paulo",Thorsons,HarperCollins Grp,"903,395","£6,370,730.30",£7.99,£7.05,Paperback,6 Sep 1999,F1.1 General & Literary Fiction 50,48,9780552996006,Notes from a Small Island,"Bryson, Bill",Black Swan,Transworld Grp,"898,388","£6,390,460.37",£8.99,£7.11,Paperback,1 Aug 1996,T8.5 Travel Writing 623,49,9780099487821,"Boy in the Striped Pyjamas,The#","Boyne, John",Definitions,Transworld Grp,"885,450","£4,557,735.53",£6.99,£5.68,Paperback,1 Feb 2007,Y2.2 Young Adult Fiction 51,50,9780141011905,Stupid White Men:...and Other Sorry Excuses for the State of the Natio,"Moore, Michael",Penguin Books,Penguin Grp,"877,777","£6,162,996.63",£7.99,£7.02,Paperback,3 Oct 2002,T7.5 Current Affairs & Issues 52,51,9780718154776,Jamie's 30-minute Meals,"Oliver, Jamie",Michael Joseph,Penguin Grp,"874,546","£11,297,761.32",£26.00,£12.92,Hardback,30 Sep 2010,T16.0 Food & Drink: General 53,52,9780099457169,"Broker,The","Grisham, John",Arrow Books,Random House Grp,"869,077","£4,216,937.77",£7.99,£4.85,Paperback,27 Aug 2005,"F2.1 Crime, Thriller & Adventure" 54,53,9780330332774,Bridget Jones's Diary:A Novel,"Fielding, Helen",Picador,Pan Macmillan Grp,"858,390","£5,310,481.76",£7.99,£6.19,Paperback,20 Jun 1997,F1.1 General & Literary Fiction 55,54,9780241003008,"Very Hungry Caterpillar,The:The Very Hungry Caterpillar","Carle, Eric",Puffin Books,Penguin Grp,"855,920","£4,101,816.11",£5.99,£4.79,Hardback,29 Sep 1994,Y1.1 Pre-school & Early Learning 56,55,9780747582977,"Thousand Splendid Suns,A","Hosseini, Khaled",Bloomsbury Publishing PLC,Bloomsbury Grp,"855,523","£8,146,523.92",£11.99,£9.52,Paperback,22 May 2007,F1.1 General & Literary Fiction 57,56,9781846051616,"Sound of Laughter,The","Kay, Peter",Century,Random House Grp,"853,499","£9,308,356.20",£18.99,£10.91,Hardback,5 Oct 2006,T4.0A Autobiography: General 58,57,9780718147709,Jamie's Italy,"Oliver, Jamie",Michael Joseph,Penguin Grp,"833,060","£9,379,860.94",£25.00,£11.26,Hardback,3 Oct 2005,T16.1 National & Regional Cuisine 77,58,9780755307500,Small Island#,"Levy, Andrea",Headline Review,Headline Grp,"831,039","£5,066,748.17",£7.99,£6.22,Paperback,13 Sep 2004,F1.1 General & Literary Fiction 59,59,9780141030142,"Memory Keeper's Daughter,The","Edwards, Kim",Penguin Books,Penguin Grp,"820,877","£4,531,503.79",£7.99,£5.52,Paperback,26 Apr 2007,F1.1 General & Literary Fiction 61,60,9780007110926,Billy Connolly,"Stephenson, Pamela",HarperCollins Publishers,HarperCollins Grp,"801,133","£4,350,571.24",£8.99,£5.43,Paperback,8 Jul 2002,T4.1 Biography: The Arts 62,61,9780330448444,"House at Riverton,The","Morton, Kate",Pan Books,Pan Macmillan Grp,"793,338","£4,268,735.29",£7.99,£5.38,Paperback,15 Jun 2007,F1.1 General & Literary Fiction 96,62,9780747561071,Harry Potter and the Order of the Phoenix#,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"788,619","£4,488,324.82",£7.99,£5.66,Paperback,10 Jul 2004,Y2.1 Children's Fiction 63,63,9780701181840,Nigella Express,"Lawson, Nigella",Chatto & Windus,Random House Grp,"780,665","£11,089,668.94",£25.00,£14.21,Hardback,6 Sep 2007,T16.0 Food & Drink: General 64,64,9780099771517,Memoirs of a Geisha,"Golden, Arthur",Vintage,Random House Grp,"766,706","£5,143,233.76",£7.99,£6.71,Paperback,4 Jun 1998,F1.1 General & Literary Fiction 65,65,9780563384311,Delia's How to Cook:(Bk.2) ,"Smith, Delia",BBC Books (Random House),Random House Grp,"765,246","£8,788,133.69",£20.00,£11.48,Hardback,9 Dec 1999,T16.0 Food & Drink: General 66,66,9780590112895,"Subtle Knife,The:His Dark Materials S.","Pullman, Philip",Point,Scholastic Ltd. Grp,"762,717","£4,335,233.24",£6.99,£5.68,Paperback,16 Oct 1998,Y2.2 Young Adult Fiction 67,67,9780718148621,Jamie's Ministry of Food:Anyone Can Learn to Cook in 24 Hours,"Oliver, Jamie",Michael Joseph,Penguin Grp,"759,900","£9,602,472.40",£25.00,£12.64,Hardback,3 Oct 2008,T16.0 Food & Drink: General 68,68,9781904994367,Guinness World Records 2009:2009,,Guinness World Records,Guinness Publisher Group,"747,715","£7,369,687.78",£20.00,£9.86,Hardback,17 Sep 2008,T2.0 Encyclopedias & General Reference 69,69,9781861978769,Why Don't Penguins' Feet Freeze?:And 114 Other Questions,,Profile Books,Profile Books Group,"746,917","£3,792,483.43",£7.99,£5.08,Paperback,5 Oct 2006,T8.0 Popular Science 70,70,9780718152437,Jamie at Home:Cook Your Way to the Good Life,"Oliver, Jamie",Michael Joseph,Penguin Grp,"741,643","£10,695,848.54",£30.00,£14.42,Hardback,6 Sep 2007,T16.0 Food & Drink: General 71,71,9780140276336,White Teeth,"Smith, Zadie",Penguin Books,Penguin Grp,"731,416","£4,601,032.02",£8.99,£6.29,Paperback,25 Jan 2001,F1.1 General & Literary Fiction 72,72,9780007156108,"Devil Wears Prada,The","Weisberger, Lauren",HarperCollins Publishers,HarperCollins Grp,"725,210","£4,077,326.77",£7.99,£5.62,Paperback,6 Oct 2003,F1.1 General & Literary Fiction 73,73,9780593059258,At My Mother's Knee ...:and Other Low Joints,"O'Grady, Paul",Bantam Press,Transworld Grp,"723,164","£7,115,648.91",£18.99,£9.84,Hardback,24 Sep 2008,T4.1A Autobiography: The Arts 74,74,9780752893686,No Time for Goodbye,"Barclay, Linwood",Orion (an Imprint of The Orion,Orion Grp,"722,103","£3,833,775.63",£7.99,£5.31,Paperback,12 Jun 2008,"F2.1 Crime, Thriller & Adventure" 75,75,9780007207329,"""Times"" Su Doku,The:The Utterly Addictive Number-placing Puzzle:(Bk. 1",,Times Books,HarperCollins Grp,"719,307","£3,532,794.40",£5.99,£4.91,Paperback,7 Mar 2005,T13.4 Puzzles 76,76,9780552998482,Chocolat,"Harris, Joanne",Black Swan,Transworld Grp,"713,299","£4,526,189.31",£7.99,£6.35,Paperback,2 Mar 2000,F1.1 General & Literary Fiction 78,77,9780718144395,"Return of the Naked Chef,The","Oliver, Jamie",Michael Joseph,Penguin Grp,"707,570","£11,156,890.07",£20.00,£15.77,Hardback,30 Mar 2000,T16.0 Food & Drink: General 80,78,9780006498407,Angela's Ashes:A Memoir of a Childhood,"McCourt, Frank",Flamingo,HarperCollins Grp,"694,831","£5,231,219.60",£7.99,£7.53,Paperback,6 May 1997,T4.0A Autobiography: General 81,79,9780747563204,Schott's Original Miscellany,"Schott, Ben",Bloomsbury Publishing PLC,Bloomsbury Grp,"694,063","£6,483,193.57",£12.99,£9.34,Hardback,4 Nov 2002,T13.3 Trivia & Quiz Books 82,80,9781847670946,Dreams from My Father:A Story of Race and Inheritance,"Obama, Barack",Canongate Books,Canongate Grp,"690,765","£4,575,110.01",£8.99,£6.62,Paperback,5 Jun 2008,"T4.2A Autobiography: Historical, Political & Military" 83,81,9780007232741,"Dangerous Book for Boys,The","Iggulden, Conn & Iggulden, Hal",HarperCollins Publishers,HarperCollins Grp,"688,808","£8,765,298.70",£20.00,£12.73,Hardback,5 Jun 2006,"T11.5 Hobbies, Pastimes & Indoor Games" 123,82,9780099419785,To Kill a Mockingbird#,"Lee, Harper",Arrow Books,Random House Grp,"682,102","£3,637,436.79",£6.99,£5.47,Paperback,5 Oct 1989,F1.1 General & Literary Fiction 84,83,9780747581109,Harry Potter and the Half-blood Prince,"Rowling, J. K.",Bloomsbury Publishing PLC,Bloomsbury Grp,"680,886","£6,506,995.60",£16.99,£9.56,Hardback,16 Jul 2005,F2.2 Science Fiction & Fantasy 85,84,9780099406136,"Summons,The","Grisham, John",Arrow Books,Random House Grp,"677,378","£3,706,035.02",£7.99,£5.47,Paperback,28 Sep 2002,"F2.1 Crime, Thriller & Adventure" 86,85,9780552149525,"Lost Symbol,The","Brown, Dan",Corgi Books,Transworld Grp,"672,950","£2,854,411.91",£7.99,£4.24,Paperback,22 Jul 2010,"F2.1 Crime, Thriller & Adventure" 87,86,9780140237504,"Catcher in the Rye,The","Salinger, J.D.",Penguin Books,Penguin Grp,"668,497","£4,474,363.25",£8.99,£6.69,Paperback,4 Aug 1994,F1.1 General & Literary Fiction 88,87,9780593050545,I Can Make You Thin,"McKenna, Paul",Bantam Press,Transworld Grp,"665,923","£5,131,848.43",£10.99,£7.71,Paperback,17 Jan 2005,T9.4 Fitness & Diet 89,88,9780718144845,Happy Days with the Naked Chef,"Oliver, Jamie",Michael Joseph,Penguin Grp,"659,722","£8,969,623.21",£20.00,£13.60,Hardback,3 Sep 2001,T16.0 Food & Drink: General 90,89,9780552771153,Brick Lane,"Ali, Monica",Black Swan,Transworld Grp,"659,023","£4,182,105.62",£7.99,£6.35,Paperback,1 May 2004,F1.1 General & Literary Fiction 91,90,9780141019376,Anybody Out There?,"Keyes, Marian",Penguin Books,Penguin Grp,"658,713","£3,401,795.62",£7.99,£5.16,Paperback,8 Feb 2007,F1.1 General & Literary Fiction 92,91,9780552772747,"Undomestic Goddess,The","Kinsella, Sophie",Black Swan,Transworld Grp,"658,598","£3,277,738.43",£7.99,£4.98,Paperback,2 Jan 2006,F1.1 General & Literary Fiction 116,92,9780552773898,"Book Thief,The#","Zusak, Markus",Black Swan,Transworld Grp,"658,524","£3,699,537.68",£7.99,£5.48,Paperback,1 Jan 2008,F1.1 General & Literary Fiction 93,93,9780141022925,I Know You Got Soul,"Clarkson, Jeremy",Penguin Books,Penguin Grp,"658,274","£3,586,065.99",£7.99,£5.45,Paperback,25 May 2006,T12.0 Transport: General 94,94,9780316731317,Sharon Osbourne Extreme:My Autobiography,"Osbourne, Sharon",Time Warner Books,"Little, Brown Book Grp","656,431","£7,086,434.17",£18.99,£10.80,Hardback,3 Oct 2005,T4.1A Autobiography: The Arts 95,95,9781904994497,Guinness World Records 2010,,Guinness World Records,Guinness Publisher Group,"656,326","£6,181,945.47",£20.00,£9.42,Hardback,17 Sep 2009,T2.0 Encyclopedias & General Reference 97,96,9780439993586,"Amber Spyglass,The:His Dark Materials S.","Pullman, Philip",Point,Scholastic Ltd. Grp,"653,615","£3,797,459.74",£6.99,£5.81,Paperback,14 Sep 2001,Y2.2 Young Adult Fiction 98,97,9780552771108,Can You Keep a Secret?,"Kinsella, Sophie",Black Swan,Transworld Grp,"652,788","£3,722,312.19",£7.99,£5.70,Paperback,17 Mar 2003,F1.1 General & Literary Fiction 99,98,9780552997034,Down Under,"Bryson, Bill",Black Swan,Transworld Grp,"646,515","£4,628,013.69",£8.99,£7.16,Paperback,6 Aug 2001,T8.5 Travel Writing 100,99,9780099506928,"Spot of Bother,A","Haddon, Mark",Vintage,Random House Grp,"645,453","£3,949,459.94",£7.99,£6.12,Paperback,7 Jun 2007,F1.1 General & Literary Fiction 101,100,9781846053443,Dear Fatty,"French, Dawn",Century,Random House Grp,"643,636","£6,330,572.88",£18.99,£9.84,Hardback,9 Oct 2008,T4.1A Autobiography: The Arts

See the example of a CSV Decoder below:

package main

import (
	"bytes"
	"encoding/csv"
	"encoding/json"
	"errors"
	"fmt"

	gojsonq "github.com/thedevsaddam/gojsonq/v2"
)

func main() {
	jq := gojsonq.New(gojsonq.SetDecoder(&csvDecoder{})).File("./data.csv")
	jq.WhereEqual("Title", "Can You Keep a Secret?")
	fmt.Printf("%v\n", jq.First())
}

type csvDecoder struct {
}

func (i *csvDecoder) Decode(data []byte, v interface{}) error {
	reader := csv.NewReader(bytes.NewReader(data))
	rr, err := reader.ReadAll()
	if err != nil {
		return errors.New("gojsonq: " + err.Error())
	}
	if len(rr) < 1 {
		return errors.New("gojsonq: csv data can't be empty! At least contain the header row")
	}
	var arr = make([]map[string]interface{}, 0)
	header := rr[0] // assume the very first row as header
	for i := 1; i <= len(rr)-1; i++ {
		if rr[i] == nil { // if a row is empty, skip it
			continue
		}
		mp := map[string]interface{}{}
		for j := 0; j < len(header); j++ {
			// convert data to different types
			// if header contains field like, ID|NUMBER,Name|String,IsStudent|BOOLEAN
			t := strings.Split(header[j], "|")
			var typ string
			if len(t) > 1 {
				typ = t[1]
			}
			hdr := strings.TrimSpace(t[0])
			switch typ {
			default:
				mp[hdr] = rr[i][j]

			case "STRING":
				mp[hdr] = rr[i][j]

			case "NUMBER":
				if fv, err := strconv.ParseFloat(rr[i][j], 64); err == nil {
					mp[hdr] = fv
				} else {
					mp[hdr] = 0.0
				}

			case "BOOLEAN":
				if strings.ToLower(rr[i][j]) == "true" ||
					rr[i][j] == "1" {
					mp[hdr] = true
				} else {
					mp[hdr] = false
				}

			}
		}
		arr = append(arr, mp)
	}
	bb, err := json.Marshal(arr)
	if err != nil {
		return fmt.Errorf("gojsonq: %v", err)
	}
	return json.Unmarshal(bb, &v)
}