gjson.go 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603
  1. // Package gjson provides searching for json strings.
  2. package gjson
  3. import (
  4. "strconv"
  5. "strings"
  6. "time"
  7. "unicode/utf16"
  8. "unicode/utf8"
  9. "unsafe"
  10. "github.com/tidwall/match"
  11. "github.com/tidwall/pretty"
  12. )
  13. // Type is Result type
  14. type Type int
  15. const (
  16. // Null is a null json value
  17. Null Type = iota
  18. // False is a json false boolean
  19. False
  20. // Number is json number
  21. Number
  22. // String is a json string
  23. String
  24. // True is a json true boolean
  25. True
  26. // JSON is a raw block of JSON
  27. JSON
  28. )
  29. // String returns a string representation of the type.
  30. func (t Type) String() string {
  31. switch t {
  32. default:
  33. return ""
  34. case Null:
  35. return "Null"
  36. case False:
  37. return "False"
  38. case Number:
  39. return "Number"
  40. case String:
  41. return "String"
  42. case True:
  43. return "True"
  44. case JSON:
  45. return "JSON"
  46. }
  47. }
  48. // Result represents a json value that is returned from Get().
  49. type Result struct {
  50. // Type is the json type
  51. Type Type
  52. // Raw is the raw json
  53. Raw string
  54. // Str is the json string
  55. Str string
  56. // Num is the json number
  57. Num float64
  58. // Index of raw value in original json, zero means index unknown
  59. Index int
  60. // Indexes of all the elements that match on a path containing the '#'
  61. // query character.
  62. Indexes []int
  63. }
  64. // String returns a string representation of the value.
  65. func (t Result) String() string {
  66. switch t.Type {
  67. default:
  68. return ""
  69. case False:
  70. return "false"
  71. case Number:
  72. if len(t.Raw) == 0 {
  73. // calculated result
  74. return strconv.FormatFloat(t.Num, 'f', -1, 64)
  75. }
  76. var i int
  77. if t.Raw[0] == '-' {
  78. i++
  79. }
  80. for ; i < len(t.Raw); i++ {
  81. if t.Raw[i] < '0' || t.Raw[i] > '9' {
  82. return strconv.FormatFloat(t.Num, 'f', -1, 64)
  83. }
  84. }
  85. return t.Raw
  86. case String:
  87. return t.Str
  88. case JSON:
  89. return t.Raw
  90. case True:
  91. return "true"
  92. }
  93. }
  94. // Bool returns an boolean representation.
  95. func (t Result) Bool() bool {
  96. switch t.Type {
  97. default:
  98. return false
  99. case True:
  100. return true
  101. case String:
  102. b, _ := strconv.ParseBool(strings.ToLower(t.Str))
  103. return b
  104. case Number:
  105. return t.Num != 0
  106. }
  107. }
  108. // Int returns an integer representation.
  109. func (t Result) Int() int64 {
  110. switch t.Type {
  111. default:
  112. return 0
  113. case True:
  114. return 1
  115. case String:
  116. n, _ := parseInt(t.Str)
  117. return n
  118. case Number:
  119. // try to directly convert the float64 to int64
  120. i, ok := safeInt(t.Num)
  121. if ok {
  122. return i
  123. }
  124. // now try to parse the raw string
  125. i, ok = parseInt(t.Raw)
  126. if ok {
  127. return i
  128. }
  129. // fallback to a standard conversion
  130. return int64(t.Num)
  131. }
  132. }
  133. // Uint returns an unsigned integer representation.
  134. func (t Result) Uint() uint64 {
  135. switch t.Type {
  136. default:
  137. return 0
  138. case True:
  139. return 1
  140. case String:
  141. n, _ := parseUint(t.Str)
  142. return n
  143. case Number:
  144. // try to directly convert the float64 to uint64
  145. i, ok := safeInt(t.Num)
  146. if ok && i >= 0 {
  147. return uint64(i)
  148. }
  149. // now try to parse the raw string
  150. u, ok := parseUint(t.Raw)
  151. if ok {
  152. return u
  153. }
  154. // fallback to a standard conversion
  155. return uint64(t.Num)
  156. }
  157. }
  158. // Float returns an float64 representation.
  159. func (t Result) Float() float64 {
  160. switch t.Type {
  161. default:
  162. return 0
  163. case True:
  164. return 1
  165. case String:
  166. n, _ := strconv.ParseFloat(t.Str, 64)
  167. return n
  168. case Number:
  169. return t.Num
  170. }
  171. }
  172. // Time returns a time.Time representation.
  173. func (t Result) Time() time.Time {
  174. res, _ := time.Parse(time.RFC3339, t.String())
  175. return res
  176. }
  177. // Array returns back an array of values.
  178. // If the result represents a null value or is non-existent, then an empty
  179. // array will be returned.
  180. // If the result is not a JSON array, the return value will be an
  181. // array containing one result.
  182. func (t Result) Array() []Result {
  183. if t.Type == Null {
  184. return []Result{}
  185. }
  186. if !t.IsArray() {
  187. return []Result{t}
  188. }
  189. r := t.arrayOrMap('[', false)
  190. return r.a
  191. }
  192. // IsObject returns true if the result value is a JSON object.
  193. func (t Result) IsObject() bool {
  194. return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{'
  195. }
  196. // IsArray returns true if the result value is a JSON array.
  197. func (t Result) IsArray() bool {
  198. return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '['
  199. }
  200. // IsBool returns true if the result value is a JSON boolean.
  201. func (t Result) IsBool() bool {
  202. return t.Type == True || t.Type == False
  203. }
  204. // ForEach iterates through values.
  205. // If the result represents a non-existent value, then no values will be
  206. // iterated. If the result is an Object, the iterator will pass the key and
  207. // value of each item. If the result is an Array, the iterator will only pass
  208. // the value of each item. If the result is not a JSON array or object, the
  209. // iterator will pass back one value equal to the result.
  210. func (t Result) ForEach(iterator func(key, value Result) bool) {
  211. if !t.Exists() {
  212. return
  213. }
  214. if t.Type != JSON {
  215. iterator(Result{}, t)
  216. return
  217. }
  218. json := t.Raw
  219. var obj bool
  220. var i int
  221. var key, value Result
  222. for ; i < len(json); i++ {
  223. if json[i] == '{' {
  224. i++
  225. key.Type = String
  226. obj = true
  227. break
  228. } else if json[i] == '[' {
  229. i++
  230. key.Type = Number
  231. key.Num = -1
  232. break
  233. }
  234. if json[i] > ' ' {
  235. return
  236. }
  237. }
  238. var str string
  239. var vesc bool
  240. var ok bool
  241. var idx int
  242. for ; i < len(json); i++ {
  243. if obj {
  244. if json[i] != '"' {
  245. continue
  246. }
  247. s := i
  248. i, str, vesc, ok = parseString(json, i+1)
  249. if !ok {
  250. return
  251. }
  252. if vesc {
  253. key.Str = unescape(str[1 : len(str)-1])
  254. } else {
  255. key.Str = str[1 : len(str)-1]
  256. }
  257. key.Raw = str
  258. key.Index = s + t.Index
  259. } else {
  260. key.Num += 1
  261. }
  262. for ; i < len(json); i++ {
  263. if json[i] <= ' ' || json[i] == ',' || json[i] == ':' {
  264. continue
  265. }
  266. break
  267. }
  268. s := i
  269. i, value, ok = parseAny(json, i, true)
  270. if !ok {
  271. return
  272. }
  273. if t.Indexes != nil {
  274. if idx < len(t.Indexes) {
  275. value.Index = t.Indexes[idx]
  276. }
  277. } else {
  278. value.Index = s + t.Index
  279. }
  280. if !iterator(key, value) {
  281. return
  282. }
  283. idx++
  284. }
  285. }
  286. // Map returns back a map of values. The result should be a JSON object.
  287. // If the result is not a JSON object, the return value will be an empty map.
  288. func (t Result) Map() map[string]Result {
  289. if t.Type != JSON {
  290. return map[string]Result{}
  291. }
  292. r := t.arrayOrMap('{', false)
  293. return r.o
  294. }
  295. // Get searches result for the specified path.
  296. // The result should be a JSON array or object.
  297. func (t Result) Get(path string) Result {
  298. r := Get(t.Raw, path)
  299. if r.Indexes != nil {
  300. for i := 0; i < len(r.Indexes); i++ {
  301. r.Indexes[i] += t.Index
  302. }
  303. } else {
  304. r.Index += t.Index
  305. }
  306. return r
  307. }
  308. type arrayOrMapResult struct {
  309. a []Result
  310. ai []interface{}
  311. o map[string]Result
  312. oi map[string]interface{}
  313. vc byte
  314. }
  315. func (t Result) arrayOrMap(vc byte, valueize bool) (r arrayOrMapResult) {
  316. var json = t.Raw
  317. var i int
  318. var value Result
  319. var count int
  320. var key Result
  321. if vc == 0 {
  322. for ; i < len(json); i++ {
  323. if json[i] == '{' || json[i] == '[' {
  324. r.vc = json[i]
  325. i++
  326. break
  327. }
  328. if json[i] > ' ' {
  329. goto end
  330. }
  331. }
  332. } else {
  333. for ; i < len(json); i++ {
  334. if json[i] == vc {
  335. i++
  336. break
  337. }
  338. if json[i] > ' ' {
  339. goto end
  340. }
  341. }
  342. r.vc = vc
  343. }
  344. if r.vc == '{' {
  345. if valueize {
  346. r.oi = make(map[string]interface{})
  347. } else {
  348. r.o = make(map[string]Result)
  349. }
  350. } else {
  351. if valueize {
  352. r.ai = make([]interface{}, 0)
  353. } else {
  354. r.a = make([]Result, 0)
  355. }
  356. }
  357. for ; i < len(json); i++ {
  358. if json[i] <= ' ' {
  359. continue
  360. }
  361. // get next value
  362. if json[i] == ']' || json[i] == '}' {
  363. break
  364. }
  365. switch json[i] {
  366. default:
  367. if (json[i] >= '0' && json[i] <= '9') || json[i] == '-' {
  368. value.Type = Number
  369. value.Raw, value.Num = tonum(json[i:])
  370. value.Str = ""
  371. } else {
  372. continue
  373. }
  374. case '{', '[':
  375. value.Type = JSON
  376. value.Raw = squash(json[i:])
  377. value.Str, value.Num = "", 0
  378. case 'n':
  379. value.Type = Null
  380. value.Raw = tolit(json[i:])
  381. value.Str, value.Num = "", 0
  382. case 't':
  383. value.Type = True
  384. value.Raw = tolit(json[i:])
  385. value.Str, value.Num = "", 0
  386. case 'f':
  387. value.Type = False
  388. value.Raw = tolit(json[i:])
  389. value.Str, value.Num = "", 0
  390. case '"':
  391. value.Type = String
  392. value.Raw, value.Str = tostr(json[i:])
  393. value.Num = 0
  394. }
  395. value.Index = i + t.Index
  396. i += len(value.Raw) - 1
  397. if r.vc == '{' {
  398. if count%2 == 0 {
  399. key = value
  400. } else {
  401. if valueize {
  402. if _, ok := r.oi[key.Str]; !ok {
  403. r.oi[key.Str] = value.Value()
  404. }
  405. } else {
  406. if _, ok := r.o[key.Str]; !ok {
  407. r.o[key.Str] = value
  408. }
  409. }
  410. }
  411. count++
  412. } else {
  413. if valueize {
  414. r.ai = append(r.ai, value.Value())
  415. } else {
  416. r.a = append(r.a, value)
  417. }
  418. }
  419. }
  420. end:
  421. if t.Indexes != nil {
  422. if len(t.Indexes) != len(r.a) {
  423. for i := 0; i < len(r.a); i++ {
  424. r.a[i].Index = 0
  425. }
  426. } else {
  427. for i := 0; i < len(r.a); i++ {
  428. r.a[i].Index = t.Indexes[i]
  429. }
  430. }
  431. }
  432. return
  433. }
  434. // Parse parses the json and returns a result.
  435. //
  436. // This function expects that the json is well-formed, and does not validate.
  437. // Invalid json will not panic, but it may return back unexpected results.
  438. // If you are consuming JSON from an unpredictable source then you may want to
  439. // use the Valid function first.
  440. func Parse(json string) Result {
  441. var value Result
  442. i := 0
  443. for ; i < len(json); i++ {
  444. if json[i] == '{' || json[i] == '[' {
  445. value.Type = JSON
  446. value.Raw = json[i:] // just take the entire raw
  447. break
  448. }
  449. if json[i] <= ' ' {
  450. continue
  451. }
  452. switch json[i] {
  453. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  454. 'i', 'I', 'N':
  455. value.Type = Number
  456. value.Raw, value.Num = tonum(json[i:])
  457. case 'n':
  458. if i+1 < len(json) && json[i+1] != 'u' {
  459. // nan
  460. value.Type = Number
  461. value.Raw, value.Num = tonum(json[i:])
  462. } else {
  463. // null
  464. value.Type = Null
  465. value.Raw = tolit(json[i:])
  466. }
  467. case 't':
  468. value.Type = True
  469. value.Raw = tolit(json[i:])
  470. case 'f':
  471. value.Type = False
  472. value.Raw = tolit(json[i:])
  473. case '"':
  474. value.Type = String
  475. value.Raw, value.Str = tostr(json[i:])
  476. default:
  477. return Result{}
  478. }
  479. break
  480. }
  481. if value.Exists() {
  482. value.Index = i
  483. }
  484. return value
  485. }
  486. // ParseBytes parses the json and returns a result.
  487. // If working with bytes, this method preferred over Parse(string(data))
  488. func ParseBytes(json []byte) Result {
  489. return Parse(string(json))
  490. }
  491. func squash(json string) string {
  492. // expects that the lead character is a '[' or '{' or '(' or '"'
  493. // squash the value, ignoring all nested arrays and objects.
  494. var i, depth int
  495. if json[0] != '"' {
  496. i, depth = 1, 1
  497. }
  498. for ; i < len(json); i++ {
  499. if json[i] >= '"' && json[i] <= '}' {
  500. switch json[i] {
  501. case '"':
  502. i++
  503. s2 := i
  504. for ; i < len(json); i++ {
  505. if json[i] > '\\' {
  506. continue
  507. }
  508. if json[i] == '"' {
  509. // look for an escaped slash
  510. if json[i-1] == '\\' {
  511. n := 0
  512. for j := i - 2; j > s2-1; j-- {
  513. if json[j] != '\\' {
  514. break
  515. }
  516. n++
  517. }
  518. if n%2 == 0 {
  519. continue
  520. }
  521. }
  522. break
  523. }
  524. }
  525. if depth == 0 {
  526. if i >= len(json) {
  527. return json
  528. }
  529. return json[:i+1]
  530. }
  531. case '{', '[', '(':
  532. depth++
  533. case '}', ']', ')':
  534. depth--
  535. if depth == 0 {
  536. return json[:i+1]
  537. }
  538. }
  539. }
  540. }
  541. return json
  542. }
  543. func tonum(json string) (raw string, num float64) {
  544. for i := 1; i < len(json); i++ {
  545. // less than dash might have valid characters
  546. if json[i] <= '-' {
  547. if json[i] <= ' ' || json[i] == ',' {
  548. // break on whitespace and comma
  549. raw = json[:i]
  550. num, _ = strconv.ParseFloat(raw, 64)
  551. return
  552. }
  553. // could be a '+' or '-'. let's assume so.
  554. } else if json[i] == ']' || json[i] == '}' {
  555. // break on ']' or '}'
  556. raw = json[:i]
  557. num, _ = strconv.ParseFloat(raw, 64)
  558. return
  559. }
  560. }
  561. raw = json
  562. num, _ = strconv.ParseFloat(raw, 64)
  563. return
  564. }
  565. func tolit(json string) (raw string) {
  566. for i := 1; i < len(json); i++ {
  567. if json[i] < 'a' || json[i] > 'z' {
  568. return json[:i]
  569. }
  570. }
  571. return json
  572. }
  573. func tostr(json string) (raw string, str string) {
  574. // expects that the lead character is a '"'
  575. for i := 1; i < len(json); i++ {
  576. if json[i] > '\\' {
  577. continue
  578. }
  579. if json[i] == '"' {
  580. return json[:i+1], json[1:i]
  581. }
  582. if json[i] == '\\' {
  583. i++
  584. for ; i < len(json); i++ {
  585. if json[i] > '\\' {
  586. continue
  587. }
  588. if json[i] == '"' {
  589. // look for an escaped slash
  590. if json[i-1] == '\\' {
  591. n := 0
  592. for j := i - 2; j > 0; j-- {
  593. if json[j] != '\\' {
  594. break
  595. }
  596. n++
  597. }
  598. if n%2 == 0 {
  599. continue
  600. }
  601. }
  602. return json[:i+1], unescape(json[1:i])
  603. }
  604. }
  605. var ret string
  606. if i+1 < len(json) {
  607. ret = json[:i+1]
  608. } else {
  609. ret = json[:i]
  610. }
  611. return ret, unescape(json[1:i])
  612. }
  613. }
  614. return json, json[1:]
  615. }
  616. // Exists returns true if value exists.
  617. //
  618. // if gjson.Get(json, "name.last").Exists(){
  619. // println("value exists")
  620. // }
  621. func (t Result) Exists() bool {
  622. return t.Type != Null || len(t.Raw) != 0
  623. }
  624. // Value returns one of these types:
  625. //
  626. // bool, for JSON booleans
  627. // float64, for JSON numbers
  628. // Number, for JSON numbers
  629. // string, for JSON string literals
  630. // nil, for JSON null
  631. // map[string]interface{}, for JSON objects
  632. // []interface{}, for JSON arrays
  633. func (t Result) Value() interface{} {
  634. if t.Type == String {
  635. return t.Str
  636. }
  637. switch t.Type {
  638. default:
  639. return nil
  640. case False:
  641. return false
  642. case Number:
  643. return t.Num
  644. case JSON:
  645. r := t.arrayOrMap(0, true)
  646. if r.vc == '{' {
  647. return r.oi
  648. } else if r.vc == '[' {
  649. return r.ai
  650. }
  651. return nil
  652. case True:
  653. return true
  654. }
  655. }
  656. func parseString(json string, i int) (int, string, bool, bool) {
  657. var s = i
  658. for ; i < len(json); i++ {
  659. if json[i] > '\\' {
  660. continue
  661. }
  662. if json[i] == '"' {
  663. return i + 1, json[s-1 : i+1], false, true
  664. }
  665. if json[i] == '\\' {
  666. i++
  667. for ; i < len(json); i++ {
  668. if json[i] > '\\' {
  669. continue
  670. }
  671. if json[i] == '"' {
  672. // look for an escaped slash
  673. if json[i-1] == '\\' {
  674. n := 0
  675. for j := i - 2; j > 0; j-- {
  676. if json[j] != '\\' {
  677. break
  678. }
  679. n++
  680. }
  681. if n%2 == 0 {
  682. continue
  683. }
  684. }
  685. return i + 1, json[s-1 : i+1], true, true
  686. }
  687. }
  688. break
  689. }
  690. }
  691. return i, json[s-1:], false, false
  692. }
  693. func parseNumber(json string, i int) (int, string) {
  694. var s = i
  695. i++
  696. for ; i < len(json); i++ {
  697. if json[i] <= ' ' || json[i] == ',' || json[i] == ']' ||
  698. json[i] == '}' {
  699. return i, json[s:i]
  700. }
  701. }
  702. return i, json[s:]
  703. }
  704. func parseLiteral(json string, i int) (int, string) {
  705. var s = i
  706. i++
  707. for ; i < len(json); i++ {
  708. if json[i] < 'a' || json[i] > 'z' {
  709. return i, json[s:i]
  710. }
  711. }
  712. return i, json[s:]
  713. }
  714. type arrayPathResult struct {
  715. part string
  716. path string
  717. pipe string
  718. piped bool
  719. more bool
  720. alogok bool
  721. arrch bool
  722. alogkey string
  723. query struct {
  724. on bool
  725. all bool
  726. path string
  727. op string
  728. value string
  729. }
  730. }
  731. func parseArrayPath(path string) (r arrayPathResult) {
  732. for i := 0; i < len(path); i++ {
  733. if path[i] == '|' {
  734. r.part = path[:i]
  735. r.pipe = path[i+1:]
  736. r.piped = true
  737. return
  738. }
  739. if path[i] == '.' {
  740. r.part = path[:i]
  741. if !r.arrch && i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  742. r.pipe = path[i+1:]
  743. r.piped = true
  744. } else {
  745. r.path = path[i+1:]
  746. r.more = true
  747. }
  748. return
  749. }
  750. if path[i] == '#' {
  751. r.arrch = true
  752. if i == 0 && len(path) > 1 {
  753. if path[1] == '.' {
  754. r.alogok = true
  755. r.alogkey = path[2:]
  756. r.path = path[:1]
  757. } else if path[1] == '[' || path[1] == '(' {
  758. // query
  759. r.query.on = true
  760. qpath, op, value, _, fi, vesc, ok :=
  761. parseQuery(path[i:])
  762. if !ok {
  763. // bad query, end now
  764. break
  765. }
  766. if len(value) >= 2 && value[0] == '"' &&
  767. value[len(value)-1] == '"' {
  768. value = value[1 : len(value)-1]
  769. if vesc {
  770. value = unescape(value)
  771. }
  772. }
  773. r.query.path = qpath
  774. r.query.op = op
  775. r.query.value = value
  776. i = fi - 1
  777. if i+1 < len(path) && path[i+1] == '#' {
  778. r.query.all = true
  779. }
  780. }
  781. }
  782. continue
  783. }
  784. }
  785. r.part = path
  786. r.path = ""
  787. return
  788. }
  789. // splitQuery takes a query and splits it into three parts:
  790. //
  791. // path, op, middle, and right.
  792. //
  793. // So for this query:
  794. //
  795. // #(first_name=="Murphy").last
  796. //
  797. // Becomes
  798. //
  799. // first_name # path
  800. // =="Murphy" # middle
  801. // .last # right
  802. //
  803. // Or,
  804. //
  805. // #(service_roles.#(=="one")).cap
  806. //
  807. // Becomes
  808. //
  809. // service_roles.#(=="one") # path
  810. // # middle
  811. // .cap # right
  812. func parseQuery(query string) (
  813. path, op, value, remain string, i int, vesc, ok bool,
  814. ) {
  815. if len(query) < 2 || query[0] != '#' ||
  816. (query[1] != '(' && query[1] != '[') {
  817. return "", "", "", "", i, false, false
  818. }
  819. i = 2
  820. j := 0 // start of value part
  821. depth := 1
  822. for ; i < len(query); i++ {
  823. if depth == 1 && j == 0 {
  824. switch query[i] {
  825. case '!', '=', '<', '>', '%':
  826. // start of the value part
  827. j = i
  828. continue
  829. }
  830. }
  831. if query[i] == '\\' {
  832. i++
  833. } else if query[i] == '[' || query[i] == '(' {
  834. depth++
  835. } else if query[i] == ']' || query[i] == ')' {
  836. depth--
  837. if depth == 0 {
  838. break
  839. }
  840. } else if query[i] == '"' {
  841. // inside selector string, balance quotes
  842. i++
  843. for ; i < len(query); i++ {
  844. if query[i] == '\\' {
  845. vesc = true
  846. i++
  847. } else if query[i] == '"' {
  848. break
  849. }
  850. }
  851. }
  852. }
  853. if depth > 0 {
  854. return "", "", "", "", i, false, false
  855. }
  856. if j > 0 {
  857. path = trim(query[2:j])
  858. value = trim(query[j:i])
  859. remain = query[i+1:]
  860. // parse the compare op from the value
  861. var opsz int
  862. switch {
  863. case len(value) == 1:
  864. opsz = 1
  865. case value[0] == '!' && value[1] == '=':
  866. opsz = 2
  867. case value[0] == '!' && value[1] == '%':
  868. opsz = 2
  869. case value[0] == '<' && value[1] == '=':
  870. opsz = 2
  871. case value[0] == '>' && value[1] == '=':
  872. opsz = 2
  873. case value[0] == '=' && value[1] == '=':
  874. value = value[1:]
  875. opsz = 1
  876. case value[0] == '<':
  877. opsz = 1
  878. case value[0] == '>':
  879. opsz = 1
  880. case value[0] == '=':
  881. opsz = 1
  882. case value[0] == '%':
  883. opsz = 1
  884. }
  885. op = value[:opsz]
  886. value = trim(value[opsz:])
  887. } else {
  888. path = trim(query[2:i])
  889. remain = query[i+1:]
  890. }
  891. return path, op, value, remain, i + 1, vesc, true
  892. }
  893. func trim(s string) string {
  894. left:
  895. if len(s) > 0 && s[0] <= ' ' {
  896. s = s[1:]
  897. goto left
  898. }
  899. right:
  900. if len(s) > 0 && s[len(s)-1] <= ' ' {
  901. s = s[:len(s)-1]
  902. goto right
  903. }
  904. return s
  905. }
  906. // peek at the next byte and see if it's a '@', '[', or '{'.
  907. func isDotPiperChar(s string) bool {
  908. if DisableModifiers {
  909. return false
  910. }
  911. c := s[0]
  912. if c == '@' {
  913. // check that the next component is *not* a modifier.
  914. i := 1
  915. for ; i < len(s); i++ {
  916. if s[i] == '.' || s[i] == '|' || s[i] == ':' {
  917. break
  918. }
  919. }
  920. _, ok := modifiers[s[1:i]]
  921. return ok
  922. }
  923. return c == '[' || c == '{'
  924. }
  925. type objectPathResult struct {
  926. part string
  927. path string
  928. pipe string
  929. piped bool
  930. wild bool
  931. more bool
  932. }
  933. func parseObjectPath(path string) (r objectPathResult) {
  934. for i := 0; i < len(path); i++ {
  935. if path[i] == '|' {
  936. r.part = path[:i]
  937. r.pipe = path[i+1:]
  938. r.piped = true
  939. return
  940. }
  941. if path[i] == '.' {
  942. r.part = path[:i]
  943. if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  944. r.pipe = path[i+1:]
  945. r.piped = true
  946. } else {
  947. r.path = path[i+1:]
  948. r.more = true
  949. }
  950. return
  951. }
  952. if path[i] == '*' || path[i] == '?' {
  953. r.wild = true
  954. continue
  955. }
  956. if path[i] == '\\' {
  957. // go into escape mode. this is a slower path that
  958. // strips off the escape character from the part.
  959. epart := []byte(path[:i])
  960. i++
  961. if i < len(path) {
  962. epart = append(epart, path[i])
  963. i++
  964. for ; i < len(path); i++ {
  965. if path[i] == '\\' {
  966. i++
  967. if i < len(path) {
  968. epart = append(epart, path[i])
  969. }
  970. continue
  971. } else if path[i] == '.' {
  972. r.part = string(epart)
  973. if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  974. r.pipe = path[i+1:]
  975. r.piped = true
  976. } else {
  977. r.path = path[i+1:]
  978. r.more = true
  979. }
  980. return
  981. } else if path[i] == '|' {
  982. r.part = string(epart)
  983. r.pipe = path[i+1:]
  984. r.piped = true
  985. return
  986. } else if path[i] == '*' || path[i] == '?' {
  987. r.wild = true
  988. }
  989. epart = append(epart, path[i])
  990. }
  991. }
  992. // append the last part
  993. r.part = string(epart)
  994. return
  995. }
  996. }
  997. r.part = path
  998. return
  999. }
  1000. var vchars = [256]byte{
  1001. '"': 2, '{': 3, '(': 3, '[': 3, '}': 1, ')': 1, ']': 1,
  1002. }
  1003. func parseSquash(json string, i int) (int, string) {
  1004. // expects that the lead character is a '[' or '{' or '('
  1005. // squash the value, ignoring all nested arrays and objects.
  1006. // the first '[' or '{' or '(' has already been read
  1007. s := i
  1008. i++
  1009. depth := 1
  1010. var c byte
  1011. for i < len(json) {
  1012. for i < len(json)-8 {
  1013. jslice := json[i : i+8]
  1014. c = vchars[jslice[0]]
  1015. if c != 0 {
  1016. i += 0
  1017. goto token
  1018. }
  1019. c = vchars[jslice[1]]
  1020. if c != 0 {
  1021. i += 1
  1022. goto token
  1023. }
  1024. c = vchars[jslice[2]]
  1025. if c != 0 {
  1026. i += 2
  1027. goto token
  1028. }
  1029. c = vchars[jslice[3]]
  1030. if c != 0 {
  1031. i += 3
  1032. goto token
  1033. }
  1034. c = vchars[jslice[4]]
  1035. if c != 0 {
  1036. i += 4
  1037. goto token
  1038. }
  1039. c = vchars[jslice[5]]
  1040. if c != 0 {
  1041. i += 5
  1042. goto token
  1043. }
  1044. c = vchars[jslice[6]]
  1045. if c != 0 {
  1046. i += 6
  1047. goto token
  1048. }
  1049. c = vchars[jslice[7]]
  1050. if c != 0 {
  1051. i += 7
  1052. goto token
  1053. }
  1054. i += 8
  1055. }
  1056. c = vchars[json[i]]
  1057. if c == 0 {
  1058. i++
  1059. continue
  1060. }
  1061. token:
  1062. if c == 2 {
  1063. // '"' string
  1064. i++
  1065. s2 := i
  1066. nextquote:
  1067. for i < len(json)-8 {
  1068. jslice := json[i : i+8]
  1069. if jslice[0] == '"' {
  1070. i += 0
  1071. goto strchkesc
  1072. }
  1073. if jslice[1] == '"' {
  1074. i += 1
  1075. goto strchkesc
  1076. }
  1077. if jslice[2] == '"' {
  1078. i += 2
  1079. goto strchkesc
  1080. }
  1081. if jslice[3] == '"' {
  1082. i += 3
  1083. goto strchkesc
  1084. }
  1085. if jslice[4] == '"' {
  1086. i += 4
  1087. goto strchkesc
  1088. }
  1089. if jslice[5] == '"' {
  1090. i += 5
  1091. goto strchkesc
  1092. }
  1093. if jslice[6] == '"' {
  1094. i += 6
  1095. goto strchkesc
  1096. }
  1097. if jslice[7] == '"' {
  1098. i += 7
  1099. goto strchkesc
  1100. }
  1101. i += 8
  1102. }
  1103. goto strchkstd
  1104. strchkesc:
  1105. if json[i-1] != '\\' {
  1106. i++
  1107. continue
  1108. }
  1109. strchkstd:
  1110. for i < len(json) {
  1111. if json[i] > '\\' || json[i] != '"' {
  1112. i++
  1113. continue
  1114. }
  1115. // look for an escaped slash
  1116. if json[i-1] == '\\' {
  1117. n := 0
  1118. for j := i - 2; j > s2-1; j-- {
  1119. if json[j] != '\\' {
  1120. break
  1121. }
  1122. n++
  1123. }
  1124. if n%2 == 0 {
  1125. i++
  1126. goto nextquote
  1127. }
  1128. }
  1129. break
  1130. }
  1131. } else {
  1132. // '{', '[', '(', '}', ']', ')'
  1133. // open close tokens
  1134. depth += int(c) - 2
  1135. if depth == 0 {
  1136. i++
  1137. return i, json[s:i]
  1138. }
  1139. }
  1140. i++
  1141. }
  1142. return i, json[s:]
  1143. }
  1144. func parseObject(c *parseContext, i int, path string) (int, bool) {
  1145. var pmatch, kesc, vesc, ok, hit bool
  1146. var key, val string
  1147. rp := parseObjectPath(path)
  1148. if !rp.more && rp.piped {
  1149. c.pipe = rp.pipe
  1150. c.piped = true
  1151. }
  1152. for i < len(c.json) {
  1153. for ; i < len(c.json); i++ {
  1154. if c.json[i] == '"' {
  1155. // parse_key_string
  1156. // this is slightly different from getting s string value
  1157. // because we don't need the outer quotes.
  1158. i++
  1159. var s = i
  1160. for ; i < len(c.json); i++ {
  1161. if c.json[i] > '\\' {
  1162. continue
  1163. }
  1164. if c.json[i] == '"' {
  1165. i, key, kesc, ok = i+1, c.json[s:i], false, true
  1166. goto parse_key_string_done
  1167. }
  1168. if c.json[i] == '\\' {
  1169. i++
  1170. for ; i < len(c.json); i++ {
  1171. if c.json[i] > '\\' {
  1172. continue
  1173. }
  1174. if c.json[i] == '"' {
  1175. // look for an escaped slash
  1176. if c.json[i-1] == '\\' {
  1177. n := 0
  1178. for j := i - 2; j > 0; j-- {
  1179. if c.json[j] != '\\' {
  1180. break
  1181. }
  1182. n++
  1183. }
  1184. if n%2 == 0 {
  1185. continue
  1186. }
  1187. }
  1188. i, key, kesc, ok = i+1, c.json[s:i], true, true
  1189. goto parse_key_string_done
  1190. }
  1191. }
  1192. break
  1193. }
  1194. }
  1195. key, kesc, ok = c.json[s:], false, false
  1196. parse_key_string_done:
  1197. break
  1198. }
  1199. if c.json[i] == '}' {
  1200. return i + 1, false
  1201. }
  1202. }
  1203. if !ok {
  1204. return i, false
  1205. }
  1206. if rp.wild {
  1207. if kesc {
  1208. pmatch = matchLimit(unescape(key), rp.part)
  1209. } else {
  1210. pmatch = matchLimit(key, rp.part)
  1211. }
  1212. } else {
  1213. if kesc {
  1214. pmatch = rp.part == unescape(key)
  1215. } else {
  1216. pmatch = rp.part == key
  1217. }
  1218. }
  1219. hit = pmatch && !rp.more
  1220. for ; i < len(c.json); i++ {
  1221. var num bool
  1222. switch c.json[i] {
  1223. default:
  1224. continue
  1225. case '"':
  1226. i++
  1227. i, val, vesc, ok = parseString(c.json, i)
  1228. if !ok {
  1229. return i, false
  1230. }
  1231. if hit {
  1232. if vesc {
  1233. c.value.Str = unescape(val[1 : len(val)-1])
  1234. } else {
  1235. c.value.Str = val[1 : len(val)-1]
  1236. }
  1237. c.value.Raw = val
  1238. c.value.Type = String
  1239. return i, true
  1240. }
  1241. case '{':
  1242. if pmatch && !hit {
  1243. i, hit = parseObject(c, i+1, rp.path)
  1244. if hit {
  1245. return i, true
  1246. }
  1247. } else {
  1248. i, val = parseSquash(c.json, i)
  1249. if hit {
  1250. c.value.Raw = val
  1251. c.value.Type = JSON
  1252. return i, true
  1253. }
  1254. }
  1255. case '[':
  1256. if pmatch && !hit {
  1257. i, hit = parseArray(c, i+1, rp.path)
  1258. if hit {
  1259. return i, true
  1260. }
  1261. } else {
  1262. i, val = parseSquash(c.json, i)
  1263. if hit {
  1264. c.value.Raw = val
  1265. c.value.Type = JSON
  1266. return i, true
  1267. }
  1268. }
  1269. case 'n':
  1270. if i+1 < len(c.json) && c.json[i+1] != 'u' {
  1271. num = true
  1272. break
  1273. }
  1274. fallthrough
  1275. case 't', 'f':
  1276. vc := c.json[i]
  1277. i, val = parseLiteral(c.json, i)
  1278. if hit {
  1279. c.value.Raw = val
  1280. switch vc {
  1281. case 't':
  1282. c.value.Type = True
  1283. case 'f':
  1284. c.value.Type = False
  1285. }
  1286. return i, true
  1287. }
  1288. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1289. 'i', 'I', 'N':
  1290. num = true
  1291. }
  1292. if num {
  1293. i, val = parseNumber(c.json, i)
  1294. if hit {
  1295. c.value.Raw = val
  1296. c.value.Type = Number
  1297. c.value.Num, _ = strconv.ParseFloat(val, 64)
  1298. return i, true
  1299. }
  1300. }
  1301. break
  1302. }
  1303. }
  1304. return i, false
  1305. }
  1306. // matchLimit will limit the complexity of the match operation to avoid ReDos
  1307. // attacks from arbitrary inputs.
  1308. // See the github.com/tidwall/match.MatchLimit function for more information.
  1309. func matchLimit(str, pattern string) bool {
  1310. matched, _ := match.MatchLimit(str, pattern, 10000)
  1311. return matched
  1312. }
  1313. func falseish(t Result) bool {
  1314. switch t.Type {
  1315. case Null:
  1316. return true
  1317. case False:
  1318. return true
  1319. case String:
  1320. b, err := strconv.ParseBool(strings.ToLower(t.Str))
  1321. if err != nil {
  1322. return false
  1323. }
  1324. return !b
  1325. case Number:
  1326. return t.Num == 0
  1327. default:
  1328. return false
  1329. }
  1330. }
  1331. func trueish(t Result) bool {
  1332. switch t.Type {
  1333. case True:
  1334. return true
  1335. case String:
  1336. b, err := strconv.ParseBool(strings.ToLower(t.Str))
  1337. if err != nil {
  1338. return false
  1339. }
  1340. return b
  1341. case Number:
  1342. return t.Num != 0
  1343. default:
  1344. return false
  1345. }
  1346. }
  1347. func nullish(t Result) bool {
  1348. return t.Type == Null
  1349. }
  1350. func queryMatches(rp *arrayPathResult, value Result) bool {
  1351. rpv := rp.query.value
  1352. if len(rpv) > 0 {
  1353. if rpv[0] == '~' {
  1354. // convert to bool
  1355. rpv = rpv[1:]
  1356. var ish, ok bool
  1357. switch rpv {
  1358. case "*":
  1359. ish, ok = value.Exists(), true
  1360. case "null":
  1361. ish, ok = nullish(value), true
  1362. case "true":
  1363. ish, ok = trueish(value), true
  1364. case "false":
  1365. ish, ok = falseish(value), true
  1366. }
  1367. if ok {
  1368. rpv = "true"
  1369. if ish {
  1370. value = Result{Type: True}
  1371. } else {
  1372. value = Result{Type: False}
  1373. }
  1374. } else {
  1375. rpv = ""
  1376. value = Result{}
  1377. }
  1378. }
  1379. }
  1380. if !value.Exists() {
  1381. return false
  1382. }
  1383. if rp.query.op == "" {
  1384. // the query is only looking for existence, such as:
  1385. // friends.#(name)
  1386. // which makes sure that the array "friends" has an element of
  1387. // "name" that exists
  1388. return true
  1389. }
  1390. switch value.Type {
  1391. case String:
  1392. switch rp.query.op {
  1393. case "=":
  1394. return value.Str == rpv
  1395. case "!=":
  1396. return value.Str != rpv
  1397. case "<":
  1398. return value.Str < rpv
  1399. case "<=":
  1400. return value.Str <= rpv
  1401. case ">":
  1402. return value.Str > rpv
  1403. case ">=":
  1404. return value.Str >= rpv
  1405. case "%":
  1406. return matchLimit(value.Str, rpv)
  1407. case "!%":
  1408. return !matchLimit(value.Str, rpv)
  1409. }
  1410. case Number:
  1411. rpvn, _ := strconv.ParseFloat(rpv, 64)
  1412. switch rp.query.op {
  1413. case "=":
  1414. return value.Num == rpvn
  1415. case "!=":
  1416. return value.Num != rpvn
  1417. case "<":
  1418. return value.Num < rpvn
  1419. case "<=":
  1420. return value.Num <= rpvn
  1421. case ">":
  1422. return value.Num > rpvn
  1423. case ">=":
  1424. return value.Num >= rpvn
  1425. }
  1426. case True:
  1427. switch rp.query.op {
  1428. case "=":
  1429. return rpv == "true"
  1430. case "!=":
  1431. return rpv != "true"
  1432. case ">":
  1433. return rpv == "false"
  1434. case ">=":
  1435. return true
  1436. }
  1437. case False:
  1438. switch rp.query.op {
  1439. case "=":
  1440. return rpv == "false"
  1441. case "!=":
  1442. return rpv != "false"
  1443. case "<":
  1444. return rpv == "true"
  1445. case "<=":
  1446. return true
  1447. }
  1448. }
  1449. return false
  1450. }
  1451. func parseArray(c *parseContext, i int, path string) (int, bool) {
  1452. var pmatch, vesc, ok, hit bool
  1453. var val string
  1454. var h int
  1455. var alog []int
  1456. var partidx int
  1457. var multires []byte
  1458. var queryIndexes []int
  1459. rp := parseArrayPath(path)
  1460. if !rp.arrch {
  1461. n, ok := parseUint(rp.part)
  1462. if !ok {
  1463. partidx = -1
  1464. } else {
  1465. partidx = int(n)
  1466. }
  1467. }
  1468. if !rp.more && rp.piped {
  1469. c.pipe = rp.pipe
  1470. c.piped = true
  1471. }
  1472. procQuery := func(qval Result) bool {
  1473. if rp.query.all {
  1474. if len(multires) == 0 {
  1475. multires = append(multires, '[')
  1476. }
  1477. }
  1478. var tmp parseContext
  1479. tmp.value = qval
  1480. fillIndex(c.json, &tmp)
  1481. parentIndex := tmp.value.Index
  1482. var res Result
  1483. if qval.Type == JSON {
  1484. res = qval.Get(rp.query.path)
  1485. } else {
  1486. if rp.query.path != "" {
  1487. return false
  1488. }
  1489. res = qval
  1490. }
  1491. if queryMatches(&rp, res) {
  1492. if rp.more {
  1493. left, right, ok := splitPossiblePipe(rp.path)
  1494. if ok {
  1495. rp.path = left
  1496. c.pipe = right
  1497. c.piped = true
  1498. }
  1499. res = qval.Get(rp.path)
  1500. } else {
  1501. res = qval
  1502. }
  1503. if rp.query.all {
  1504. raw := res.Raw
  1505. if len(raw) == 0 {
  1506. raw = res.String()
  1507. }
  1508. if raw != "" {
  1509. if len(multires) > 1 {
  1510. multires = append(multires, ',')
  1511. }
  1512. multires = append(multires, raw...)
  1513. queryIndexes = append(queryIndexes, res.Index+parentIndex)
  1514. }
  1515. } else {
  1516. c.value = res
  1517. return true
  1518. }
  1519. }
  1520. return false
  1521. }
  1522. for i < len(c.json)+1 {
  1523. if !rp.arrch {
  1524. pmatch = partidx == h
  1525. hit = pmatch && !rp.more
  1526. }
  1527. h++
  1528. if rp.alogok {
  1529. alog = append(alog, i)
  1530. }
  1531. for ; ; i++ {
  1532. var ch byte
  1533. if i > len(c.json) {
  1534. break
  1535. } else if i == len(c.json) {
  1536. ch = ']'
  1537. } else {
  1538. ch = c.json[i]
  1539. }
  1540. var num bool
  1541. switch ch {
  1542. default:
  1543. continue
  1544. case '"':
  1545. i++
  1546. i, val, vesc, ok = parseString(c.json, i)
  1547. if !ok {
  1548. return i, false
  1549. }
  1550. if rp.query.on {
  1551. var qval Result
  1552. if vesc {
  1553. qval.Str = unescape(val[1 : len(val)-1])
  1554. } else {
  1555. qval.Str = val[1 : len(val)-1]
  1556. }
  1557. qval.Raw = val
  1558. qval.Type = String
  1559. if procQuery(qval) {
  1560. return i, true
  1561. }
  1562. } else if hit {
  1563. if rp.alogok {
  1564. break
  1565. }
  1566. if vesc {
  1567. c.value.Str = unescape(val[1 : len(val)-1])
  1568. } else {
  1569. c.value.Str = val[1 : len(val)-1]
  1570. }
  1571. c.value.Raw = val
  1572. c.value.Type = String
  1573. return i, true
  1574. }
  1575. case '{':
  1576. if pmatch && !hit {
  1577. i, hit = parseObject(c, i+1, rp.path)
  1578. if hit {
  1579. if rp.alogok {
  1580. break
  1581. }
  1582. return i, true
  1583. }
  1584. } else {
  1585. i, val = parseSquash(c.json, i)
  1586. if rp.query.on {
  1587. if procQuery(Result{Raw: val, Type: JSON}) {
  1588. return i, true
  1589. }
  1590. } else if hit {
  1591. if rp.alogok {
  1592. break
  1593. }
  1594. c.value.Raw = val
  1595. c.value.Type = JSON
  1596. return i, true
  1597. }
  1598. }
  1599. case '[':
  1600. if pmatch && !hit {
  1601. i, hit = parseArray(c, i+1, rp.path)
  1602. if hit {
  1603. if rp.alogok {
  1604. break
  1605. }
  1606. return i, true
  1607. }
  1608. } else {
  1609. i, val = parseSquash(c.json, i)
  1610. if rp.query.on {
  1611. if procQuery(Result{Raw: val, Type: JSON}) {
  1612. return i, true
  1613. }
  1614. } else if hit {
  1615. if rp.alogok {
  1616. break
  1617. }
  1618. c.value.Raw = val
  1619. c.value.Type = JSON
  1620. return i, true
  1621. }
  1622. }
  1623. case 'n':
  1624. if i+1 < len(c.json) && c.json[i+1] != 'u' {
  1625. num = true
  1626. break
  1627. }
  1628. fallthrough
  1629. case 't', 'f':
  1630. vc := c.json[i]
  1631. i, val = parseLiteral(c.json, i)
  1632. if rp.query.on {
  1633. var qval Result
  1634. qval.Raw = val
  1635. switch vc {
  1636. case 't':
  1637. qval.Type = True
  1638. case 'f':
  1639. qval.Type = False
  1640. }
  1641. if procQuery(qval) {
  1642. return i, true
  1643. }
  1644. } else if hit {
  1645. if rp.alogok {
  1646. break
  1647. }
  1648. c.value.Raw = val
  1649. switch vc {
  1650. case 't':
  1651. c.value.Type = True
  1652. case 'f':
  1653. c.value.Type = False
  1654. }
  1655. return i, true
  1656. }
  1657. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1658. 'i', 'I', 'N':
  1659. num = true
  1660. case ']':
  1661. if rp.arrch && rp.part == "#" {
  1662. if rp.alogok {
  1663. left, right, ok := splitPossiblePipe(rp.alogkey)
  1664. if ok {
  1665. rp.alogkey = left
  1666. c.pipe = right
  1667. c.piped = true
  1668. }
  1669. var indexes = make([]int, 0, 64)
  1670. var jsons = make([]byte, 0, 64)
  1671. jsons = append(jsons, '[')
  1672. for j, k := 0, 0; j < len(alog); j++ {
  1673. idx := alog[j]
  1674. for idx < len(c.json) {
  1675. switch c.json[idx] {
  1676. case ' ', '\t', '\r', '\n':
  1677. idx++
  1678. continue
  1679. }
  1680. break
  1681. }
  1682. if idx < len(c.json) && c.json[idx] != ']' {
  1683. _, res, ok := parseAny(c.json, idx, true)
  1684. if ok {
  1685. res := res.Get(rp.alogkey)
  1686. if res.Exists() {
  1687. if k > 0 {
  1688. jsons = append(jsons, ',')
  1689. }
  1690. raw := res.Raw
  1691. if len(raw) == 0 {
  1692. raw = res.String()
  1693. }
  1694. jsons = append(jsons, []byte(raw)...)
  1695. indexes = append(indexes, res.Index)
  1696. k++
  1697. }
  1698. }
  1699. }
  1700. }
  1701. jsons = append(jsons, ']')
  1702. c.value.Type = JSON
  1703. c.value.Raw = string(jsons)
  1704. c.value.Indexes = indexes
  1705. return i + 1, true
  1706. }
  1707. if rp.alogok {
  1708. break
  1709. }
  1710. c.value.Type = Number
  1711. c.value.Num = float64(h - 1)
  1712. c.value.Raw = strconv.Itoa(h - 1)
  1713. c.calcd = true
  1714. return i + 1, true
  1715. }
  1716. if !c.value.Exists() {
  1717. if len(multires) > 0 {
  1718. c.value = Result{
  1719. Raw: string(append(multires, ']')),
  1720. Type: JSON,
  1721. Indexes: queryIndexes,
  1722. }
  1723. } else if rp.query.all {
  1724. c.value = Result{
  1725. Raw: "[]",
  1726. Type: JSON,
  1727. }
  1728. }
  1729. }
  1730. return i + 1, false
  1731. }
  1732. if num {
  1733. i, val = parseNumber(c.json, i)
  1734. if rp.query.on {
  1735. var qval Result
  1736. qval.Raw = val
  1737. qval.Type = Number
  1738. qval.Num, _ = strconv.ParseFloat(val, 64)
  1739. if procQuery(qval) {
  1740. return i, true
  1741. }
  1742. } else if hit {
  1743. if rp.alogok {
  1744. break
  1745. }
  1746. c.value.Raw = val
  1747. c.value.Type = Number
  1748. c.value.Num, _ = strconv.ParseFloat(val, 64)
  1749. return i, true
  1750. }
  1751. }
  1752. break
  1753. }
  1754. }
  1755. return i, false
  1756. }
  1757. func splitPossiblePipe(path string) (left, right string, ok bool) {
  1758. // take a quick peek for the pipe character. If found we'll split the piped
  1759. // part of the path into the c.pipe field and shorten the rp.
  1760. var possible bool
  1761. for i := 0; i < len(path); i++ {
  1762. if path[i] == '|' {
  1763. possible = true
  1764. break
  1765. }
  1766. }
  1767. if !possible {
  1768. return
  1769. }
  1770. if len(path) > 0 && path[0] == '{' {
  1771. squashed := squash(path[1:])
  1772. if len(squashed) < len(path)-1 {
  1773. squashed = path[:len(squashed)+1]
  1774. remain := path[len(squashed):]
  1775. if remain[0] == '|' {
  1776. return squashed, remain[1:], true
  1777. }
  1778. }
  1779. return
  1780. }
  1781. // split the left and right side of the path with the pipe character as
  1782. // the delimiter. This is a little tricky because we'll need to basically
  1783. // parse the entire path.
  1784. for i := 0; i < len(path); i++ {
  1785. if path[i] == '\\' {
  1786. i++
  1787. } else if path[i] == '.' {
  1788. if i == len(path)-1 {
  1789. return
  1790. }
  1791. if path[i+1] == '#' {
  1792. i += 2
  1793. if i == len(path) {
  1794. return
  1795. }
  1796. if path[i] == '[' || path[i] == '(' {
  1797. var start, end byte
  1798. if path[i] == '[' {
  1799. start, end = '[', ']'
  1800. } else {
  1801. start, end = '(', ')'
  1802. }
  1803. // inside selector, balance brackets
  1804. i++
  1805. depth := 1
  1806. for ; i < len(path); i++ {
  1807. if path[i] == '\\' {
  1808. i++
  1809. } else if path[i] == start {
  1810. depth++
  1811. } else if path[i] == end {
  1812. depth--
  1813. if depth == 0 {
  1814. break
  1815. }
  1816. } else if path[i] == '"' {
  1817. // inside selector string, balance quotes
  1818. i++
  1819. for ; i < len(path); i++ {
  1820. if path[i] == '\\' {
  1821. i++
  1822. } else if path[i] == '"' {
  1823. break
  1824. }
  1825. }
  1826. }
  1827. }
  1828. }
  1829. }
  1830. } else if path[i] == '|' {
  1831. return path[:i], path[i+1:], true
  1832. }
  1833. }
  1834. return
  1835. }
  1836. // ForEachLine iterates through lines of JSON as specified by the JSON Lines
  1837. // format (http://jsonlines.org/).
  1838. // Each line is returned as a GJSON Result.
  1839. func ForEachLine(json string, iterator func(line Result) bool) {
  1840. var res Result
  1841. var i int
  1842. for {
  1843. i, res, _ = parseAny(json, i, true)
  1844. if !res.Exists() {
  1845. break
  1846. }
  1847. if !iterator(res) {
  1848. return
  1849. }
  1850. }
  1851. }
  1852. type subSelector struct {
  1853. name string
  1854. path string
  1855. }
  1856. // parseSubSelectors returns the subselectors belonging to a '[path1,path2]' or
  1857. // '{"field1":path1,"field2":path2}' type subSelection. It's expected that the
  1858. // first character in path is either '[' or '{', and has already been checked
  1859. // prior to calling this function.
  1860. func parseSubSelectors(path string) (sels []subSelector, out string, ok bool) {
  1861. modifier := 0
  1862. depth := 1
  1863. colon := 0
  1864. start := 1
  1865. i := 1
  1866. pushSel := func() {
  1867. var sel subSelector
  1868. if colon == 0 {
  1869. sel.path = path[start:i]
  1870. } else {
  1871. sel.name = path[start:colon]
  1872. sel.path = path[colon+1 : i]
  1873. }
  1874. sels = append(sels, sel)
  1875. colon = 0
  1876. modifier = 0
  1877. start = i + 1
  1878. }
  1879. for ; i < len(path); i++ {
  1880. switch path[i] {
  1881. case '\\':
  1882. i++
  1883. case '@':
  1884. if modifier == 0 && i > 0 && (path[i-1] == '.' || path[i-1] == '|') {
  1885. modifier = i
  1886. }
  1887. case ':':
  1888. if modifier == 0 && colon == 0 && depth == 1 {
  1889. colon = i
  1890. }
  1891. case ',':
  1892. if depth == 1 {
  1893. pushSel()
  1894. }
  1895. case '"':
  1896. i++
  1897. loop:
  1898. for ; i < len(path); i++ {
  1899. switch path[i] {
  1900. case '\\':
  1901. i++
  1902. case '"':
  1903. break loop
  1904. }
  1905. }
  1906. case '[', '(', '{':
  1907. depth++
  1908. case ']', ')', '}':
  1909. depth--
  1910. if depth == 0 {
  1911. pushSel()
  1912. path = path[i+1:]
  1913. return sels, path, true
  1914. }
  1915. }
  1916. }
  1917. return
  1918. }
  1919. // nameOfLast returns the name of the last component
  1920. func nameOfLast(path string) string {
  1921. for i := len(path) - 1; i >= 0; i-- {
  1922. if path[i] == '|' || path[i] == '.' {
  1923. if i > 0 {
  1924. if path[i-1] == '\\' {
  1925. continue
  1926. }
  1927. }
  1928. return path[i+1:]
  1929. }
  1930. }
  1931. return path
  1932. }
  1933. func isSimpleName(component string) bool {
  1934. for i := 0; i < len(component); i++ {
  1935. if component[i] < ' ' {
  1936. return false
  1937. }
  1938. switch component[i] {
  1939. case '[', ']', '{', '}', '(', ')', '#', '|', '!':
  1940. return false
  1941. }
  1942. }
  1943. return true
  1944. }
  1945. var hexchars = [...]byte{
  1946. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1947. 'a', 'b', 'c', 'd', 'e', 'f',
  1948. }
  1949. func appendHex16(dst []byte, x uint16) []byte {
  1950. return append(dst,
  1951. hexchars[x>>12&0xF], hexchars[x>>8&0xF],
  1952. hexchars[x>>4&0xF], hexchars[x>>0&0xF],
  1953. )
  1954. }
  1955. // DisableEscapeHTML will disable the automatic escaping of certain
  1956. // "problamatic" HTML characters when encoding to JSON.
  1957. // These character include '>', '<' and '&', which get escaped to \u003e,
  1958. // \u0026, and \u003c respectively.
  1959. //
  1960. // This is a global flag and will affect all further gjson operations.
  1961. // Ideally, if used, it should be set one time before other gjson functions
  1962. // are called.
  1963. var DisableEscapeHTML = false
  1964. // AppendJSONString is a convenience function that converts the provided string
  1965. // to a valid JSON string and appends it to dst.
  1966. func AppendJSONString(dst []byte, s string) []byte {
  1967. dst = append(dst, make([]byte, len(s)+2)...)
  1968. dst = append(dst[:len(dst)-len(s)-2], '"')
  1969. for i := 0; i < len(s); i++ {
  1970. if s[i] < ' ' {
  1971. dst = append(dst, '\\')
  1972. switch s[i] {
  1973. case '\b':
  1974. dst = append(dst, 'b')
  1975. case '\f':
  1976. dst = append(dst, 'f')
  1977. case '\n':
  1978. dst = append(dst, 'n')
  1979. case '\r':
  1980. dst = append(dst, 'r')
  1981. case '\t':
  1982. dst = append(dst, 't')
  1983. default:
  1984. dst = append(dst, 'u')
  1985. dst = appendHex16(dst, uint16(s[i]))
  1986. }
  1987. } else if !DisableEscapeHTML &&
  1988. (s[i] == '>' || s[i] == '<' || s[i] == '&') {
  1989. dst = append(dst, '\\', 'u')
  1990. dst = appendHex16(dst, uint16(s[i]))
  1991. } else if s[i] == '\\' {
  1992. dst = append(dst, '\\', '\\')
  1993. } else if s[i] == '"' {
  1994. dst = append(dst, '\\', '"')
  1995. } else if s[i] > 127 {
  1996. // read utf8 character
  1997. r, n := utf8.DecodeRuneInString(s[i:])
  1998. if n == 0 {
  1999. break
  2000. }
  2001. if r == utf8.RuneError && n == 1 {
  2002. dst = append(dst, `\ufffd`...)
  2003. } else if r == '\u2028' || r == '\u2029' {
  2004. dst = append(dst, `\u202`...)
  2005. dst = append(dst, hexchars[r&0xF])
  2006. } else {
  2007. dst = append(dst, s[i:i+n]...)
  2008. }
  2009. i = i + n - 1
  2010. } else {
  2011. dst = append(dst, s[i])
  2012. }
  2013. }
  2014. return append(dst, '"')
  2015. }
  2016. type parseContext struct {
  2017. json string
  2018. value Result
  2019. pipe string
  2020. piped bool
  2021. calcd bool
  2022. lines bool
  2023. }
  2024. // Get searches json for the specified path.
  2025. // A path is in dot syntax, such as "name.last" or "age".
  2026. // When the value is found it's returned immediately.
  2027. //
  2028. // A path is a series of keys separated by a dot.
  2029. // A key may contain special wildcard characters '*' and '?'.
  2030. // To access an array value use the index as the key.
  2031. // To get the number of elements in an array or to access a child path, use
  2032. // the '#' character.
  2033. // The dot and wildcard character can be escaped with '\'.
  2034. //
  2035. // {
  2036. // "name": {"first": "Tom", "last": "Anderson"},
  2037. // "age":37,
  2038. // "children": ["Sara","Alex","Jack"],
  2039. // "friends": [
  2040. // {"first": "James", "last": "Murphy"},
  2041. // {"first": "Roger", "last": "Craig"}
  2042. // ]
  2043. // }
  2044. // "name.last" >> "Anderson"
  2045. // "age" >> 37
  2046. // "children" >> ["Sara","Alex","Jack"]
  2047. // "children.#" >> 3
  2048. // "children.1" >> "Alex"
  2049. // "child*.2" >> "Jack"
  2050. // "c?ildren.0" >> "Sara"
  2051. // "friends.#.first" >> ["James","Roger"]
  2052. //
  2053. // This function expects that the json is well-formed, and does not validate.
  2054. // Invalid json will not panic, but it may return back unexpected results.
  2055. // If you are consuming JSON from an unpredictable source then you may want to
  2056. // use the Valid function first.
  2057. func Get(json, path string) Result {
  2058. if len(path) > 1 {
  2059. if (path[0] == '@' && !DisableModifiers) || path[0] == '!' {
  2060. // possible modifier
  2061. var ok bool
  2062. var npath string
  2063. var rjson string
  2064. if path[0] == '@' && !DisableModifiers {
  2065. npath, rjson, ok = execModifier(json, path)
  2066. } else if path[0] == '!' {
  2067. npath, rjson, ok = execStatic(json, path)
  2068. }
  2069. if ok {
  2070. path = npath
  2071. if len(path) > 0 && (path[0] == '|' || path[0] == '.') {
  2072. res := Get(rjson, path[1:])
  2073. res.Index = 0
  2074. res.Indexes = nil
  2075. return res
  2076. }
  2077. return Parse(rjson)
  2078. }
  2079. }
  2080. if path[0] == '[' || path[0] == '{' {
  2081. // using a subselector path
  2082. kind := path[0]
  2083. var ok bool
  2084. var subs []subSelector
  2085. subs, path, ok = parseSubSelectors(path)
  2086. if ok {
  2087. if len(path) == 0 || (path[0] == '|' || path[0] == '.') {
  2088. var b []byte
  2089. b = append(b, kind)
  2090. var i int
  2091. for _, sub := range subs {
  2092. res := Get(json, sub.path)
  2093. if res.Exists() {
  2094. if i > 0 {
  2095. b = append(b, ',')
  2096. }
  2097. if kind == '{' {
  2098. if len(sub.name) > 0 {
  2099. if sub.name[0] == '"' && Valid(sub.name) {
  2100. b = append(b, sub.name...)
  2101. } else {
  2102. b = AppendJSONString(b, sub.name)
  2103. }
  2104. } else {
  2105. last := nameOfLast(sub.path)
  2106. if isSimpleName(last) {
  2107. b = AppendJSONString(b, last)
  2108. } else {
  2109. b = AppendJSONString(b, "_")
  2110. }
  2111. }
  2112. b = append(b, ':')
  2113. }
  2114. var raw string
  2115. if len(res.Raw) == 0 {
  2116. raw = res.String()
  2117. if len(raw) == 0 {
  2118. raw = "null"
  2119. }
  2120. } else {
  2121. raw = res.Raw
  2122. }
  2123. b = append(b, raw...)
  2124. i++
  2125. }
  2126. }
  2127. b = append(b, kind+2)
  2128. var res Result
  2129. res.Raw = string(b)
  2130. res.Type = JSON
  2131. if len(path) > 0 {
  2132. res = res.Get(path[1:])
  2133. }
  2134. res.Index = 0
  2135. return res
  2136. }
  2137. }
  2138. }
  2139. }
  2140. var i int
  2141. var c = &parseContext{json: json}
  2142. if len(path) >= 2 && path[0] == '.' && path[1] == '.' {
  2143. c.lines = true
  2144. parseArray(c, 0, path[2:])
  2145. } else {
  2146. for ; i < len(c.json); i++ {
  2147. if c.json[i] == '{' {
  2148. i++
  2149. parseObject(c, i, path)
  2150. break
  2151. }
  2152. if c.json[i] == '[' {
  2153. i++
  2154. parseArray(c, i, path)
  2155. break
  2156. }
  2157. }
  2158. }
  2159. if c.piped {
  2160. res := c.value.Get(c.pipe)
  2161. res.Index = 0
  2162. return res
  2163. }
  2164. fillIndex(json, c)
  2165. return c.value
  2166. }
  2167. // GetBytes searches json for the specified path.
  2168. // If working with bytes, this method preferred over Get(string(data), path)
  2169. func GetBytes(json []byte, path string) Result {
  2170. return getBytes(json, path)
  2171. }
  2172. // runeit returns the rune from the the \uXXXX
  2173. func runeit(json string) rune {
  2174. n, _ := strconv.ParseUint(json[:4], 16, 64)
  2175. return rune(n)
  2176. }
  2177. // unescape unescapes a string
  2178. func unescape(json string) string {
  2179. var str = make([]byte, 0, len(json))
  2180. for i := 0; i < len(json); i++ {
  2181. switch {
  2182. default:
  2183. str = append(str, json[i])
  2184. case json[i] < ' ':
  2185. return string(str)
  2186. case json[i] == '\\':
  2187. i++
  2188. if i >= len(json) {
  2189. return string(str)
  2190. }
  2191. switch json[i] {
  2192. default:
  2193. return string(str)
  2194. case '\\':
  2195. str = append(str, '\\')
  2196. case '/':
  2197. str = append(str, '/')
  2198. case 'b':
  2199. str = append(str, '\b')
  2200. case 'f':
  2201. str = append(str, '\f')
  2202. case 'n':
  2203. str = append(str, '\n')
  2204. case 'r':
  2205. str = append(str, '\r')
  2206. case 't':
  2207. str = append(str, '\t')
  2208. case '"':
  2209. str = append(str, '"')
  2210. case 'u':
  2211. if i+5 > len(json) {
  2212. return string(str)
  2213. }
  2214. r := runeit(json[i+1:])
  2215. i += 5
  2216. if utf16.IsSurrogate(r) {
  2217. // need another code
  2218. if len(json[i:]) >= 6 && json[i] == '\\' &&
  2219. json[i+1] == 'u' {
  2220. // we expect it to be correct so just consume it
  2221. r = utf16.DecodeRune(r, runeit(json[i+2:]))
  2222. i += 6
  2223. }
  2224. }
  2225. // provide enough space to encode the largest utf8 possible
  2226. str = append(str, 0, 0, 0, 0, 0, 0, 0, 0)
  2227. n := utf8.EncodeRune(str[len(str)-8:], r)
  2228. str = str[:len(str)-8+n]
  2229. i-- // backtrack index by one
  2230. }
  2231. }
  2232. }
  2233. return string(str)
  2234. }
  2235. // Less return true if a token is less than another token.
  2236. // The caseSensitive parameter is used when the tokens are Strings.
  2237. // The order when comparing two different type is:
  2238. //
  2239. // Null < False < Number < String < True < JSON
  2240. func (t Result) Less(token Result, caseSensitive bool) bool {
  2241. if t.Type < token.Type {
  2242. return true
  2243. }
  2244. if t.Type > token.Type {
  2245. return false
  2246. }
  2247. if t.Type == String {
  2248. if caseSensitive {
  2249. return t.Str < token.Str
  2250. }
  2251. return stringLessInsensitive(t.Str, token.Str)
  2252. }
  2253. if t.Type == Number {
  2254. return t.Num < token.Num
  2255. }
  2256. return t.Raw < token.Raw
  2257. }
  2258. func stringLessInsensitive(a, b string) bool {
  2259. for i := 0; i < len(a) && i < len(b); i++ {
  2260. if a[i] >= 'A' && a[i] <= 'Z' {
  2261. if b[i] >= 'A' && b[i] <= 'Z' {
  2262. // both are uppercase, do nothing
  2263. if a[i] < b[i] {
  2264. return true
  2265. } else if a[i] > b[i] {
  2266. return false
  2267. }
  2268. } else {
  2269. // a is uppercase, convert a to lowercase
  2270. if a[i]+32 < b[i] {
  2271. return true
  2272. } else if a[i]+32 > b[i] {
  2273. return false
  2274. }
  2275. }
  2276. } else if b[i] >= 'A' && b[i] <= 'Z' {
  2277. // b is uppercase, convert b to lowercase
  2278. if a[i] < b[i]+32 {
  2279. return true
  2280. } else if a[i] > b[i]+32 {
  2281. return false
  2282. }
  2283. } else {
  2284. // neither are uppercase
  2285. if a[i] < b[i] {
  2286. return true
  2287. } else if a[i] > b[i] {
  2288. return false
  2289. }
  2290. }
  2291. }
  2292. return len(a) < len(b)
  2293. }
  2294. // parseAny parses the next value from a json string.
  2295. // A Result is returned when the hit param is set.
  2296. // The return values are (i int, res Result, ok bool)
  2297. func parseAny(json string, i int, hit bool) (int, Result, bool) {
  2298. var res Result
  2299. var val string
  2300. for ; i < len(json); i++ {
  2301. if json[i] == '{' || json[i] == '[' {
  2302. i, val = parseSquash(json, i)
  2303. if hit {
  2304. res.Raw = val
  2305. res.Type = JSON
  2306. }
  2307. var tmp parseContext
  2308. tmp.value = res
  2309. fillIndex(json, &tmp)
  2310. return i, tmp.value, true
  2311. }
  2312. if json[i] <= ' ' {
  2313. continue
  2314. }
  2315. var num bool
  2316. switch json[i] {
  2317. case '"':
  2318. i++
  2319. var vesc bool
  2320. var ok bool
  2321. i, val, vesc, ok = parseString(json, i)
  2322. if !ok {
  2323. return i, res, false
  2324. }
  2325. if hit {
  2326. res.Type = String
  2327. res.Raw = val
  2328. if vesc {
  2329. res.Str = unescape(val[1 : len(val)-1])
  2330. } else {
  2331. res.Str = val[1 : len(val)-1]
  2332. }
  2333. }
  2334. return i, res, true
  2335. case 'n':
  2336. if i+1 < len(json) && json[i+1] != 'u' {
  2337. num = true
  2338. break
  2339. }
  2340. fallthrough
  2341. case 't', 'f':
  2342. vc := json[i]
  2343. i, val = parseLiteral(json, i)
  2344. if hit {
  2345. res.Raw = val
  2346. switch vc {
  2347. case 't':
  2348. res.Type = True
  2349. case 'f':
  2350. res.Type = False
  2351. }
  2352. return i, res, true
  2353. }
  2354. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  2355. 'i', 'I', 'N':
  2356. num = true
  2357. }
  2358. if num {
  2359. i, val = parseNumber(json, i)
  2360. if hit {
  2361. res.Raw = val
  2362. res.Type = Number
  2363. res.Num, _ = strconv.ParseFloat(val, 64)
  2364. }
  2365. return i, res, true
  2366. }
  2367. }
  2368. return i, res, false
  2369. }
  2370. // GetMany searches json for the multiple paths.
  2371. // The return value is a Result array where the number of items
  2372. // will be equal to the number of input paths.
  2373. func GetMany(json string, path ...string) []Result {
  2374. res := make([]Result, len(path))
  2375. for i, path := range path {
  2376. res[i] = Get(json, path)
  2377. }
  2378. return res
  2379. }
  2380. // GetManyBytes searches json for the multiple paths.
  2381. // The return value is a Result array where the number of items
  2382. // will be equal to the number of input paths.
  2383. func GetManyBytes(json []byte, path ...string) []Result {
  2384. res := make([]Result, len(path))
  2385. for i, path := range path {
  2386. res[i] = GetBytes(json, path)
  2387. }
  2388. return res
  2389. }
  2390. func validpayload(data []byte, i int) (outi int, ok bool) {
  2391. for ; i < len(data); i++ {
  2392. switch data[i] {
  2393. default:
  2394. i, ok = validany(data, i)
  2395. if !ok {
  2396. return i, false
  2397. }
  2398. for ; i < len(data); i++ {
  2399. switch data[i] {
  2400. default:
  2401. return i, false
  2402. case ' ', '\t', '\n', '\r':
  2403. continue
  2404. }
  2405. }
  2406. return i, true
  2407. case ' ', '\t', '\n', '\r':
  2408. continue
  2409. }
  2410. }
  2411. return i, false
  2412. }
  2413. func validany(data []byte, i int) (outi int, ok bool) {
  2414. for ; i < len(data); i++ {
  2415. switch data[i] {
  2416. default:
  2417. return i, false
  2418. case ' ', '\t', '\n', '\r':
  2419. continue
  2420. case '{':
  2421. return validobject(data, i+1)
  2422. case '[':
  2423. return validarray(data, i+1)
  2424. case '"':
  2425. return validstring(data, i+1)
  2426. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  2427. return validnumber(data, i+1)
  2428. case 't':
  2429. return validtrue(data, i+1)
  2430. case 'f':
  2431. return validfalse(data, i+1)
  2432. case 'n':
  2433. return validnull(data, i+1)
  2434. }
  2435. }
  2436. return i, false
  2437. }
  2438. func validobject(data []byte, i int) (outi int, ok bool) {
  2439. for ; i < len(data); i++ {
  2440. switch data[i] {
  2441. default:
  2442. return i, false
  2443. case ' ', '\t', '\n', '\r':
  2444. continue
  2445. case '}':
  2446. return i + 1, true
  2447. case '"':
  2448. key:
  2449. if i, ok = validstring(data, i+1); !ok {
  2450. return i, false
  2451. }
  2452. if i, ok = validcolon(data, i); !ok {
  2453. return i, false
  2454. }
  2455. if i, ok = validany(data, i); !ok {
  2456. return i, false
  2457. }
  2458. if i, ok = validcomma(data, i, '}'); !ok {
  2459. return i, false
  2460. }
  2461. if data[i] == '}' {
  2462. return i + 1, true
  2463. }
  2464. i++
  2465. for ; i < len(data); i++ {
  2466. switch data[i] {
  2467. default:
  2468. return i, false
  2469. case ' ', '\t', '\n', '\r':
  2470. continue
  2471. case '"':
  2472. goto key
  2473. }
  2474. }
  2475. return i, false
  2476. }
  2477. }
  2478. return i, false
  2479. }
  2480. func validcolon(data []byte, i int) (outi int, ok bool) {
  2481. for ; i < len(data); i++ {
  2482. switch data[i] {
  2483. default:
  2484. return i, false
  2485. case ' ', '\t', '\n', '\r':
  2486. continue
  2487. case ':':
  2488. return i + 1, true
  2489. }
  2490. }
  2491. return i, false
  2492. }
  2493. func validcomma(data []byte, i int, end byte) (outi int, ok bool) {
  2494. for ; i < len(data); i++ {
  2495. switch data[i] {
  2496. default:
  2497. return i, false
  2498. case ' ', '\t', '\n', '\r':
  2499. continue
  2500. case ',':
  2501. return i, true
  2502. case end:
  2503. return i, true
  2504. }
  2505. }
  2506. return i, false
  2507. }
  2508. func validarray(data []byte, i int) (outi int, ok bool) {
  2509. for ; i < len(data); i++ {
  2510. switch data[i] {
  2511. default:
  2512. for ; i < len(data); i++ {
  2513. if i, ok = validany(data, i); !ok {
  2514. return i, false
  2515. }
  2516. if i, ok = validcomma(data, i, ']'); !ok {
  2517. return i, false
  2518. }
  2519. if data[i] == ']' {
  2520. return i + 1, true
  2521. }
  2522. }
  2523. case ' ', '\t', '\n', '\r':
  2524. continue
  2525. case ']':
  2526. return i + 1, true
  2527. }
  2528. }
  2529. return i, false
  2530. }
  2531. func validstring(data []byte, i int) (outi int, ok bool) {
  2532. for ; i < len(data); i++ {
  2533. if data[i] < ' ' {
  2534. return i, false
  2535. } else if data[i] == '\\' {
  2536. i++
  2537. if i == len(data) {
  2538. return i, false
  2539. }
  2540. switch data[i] {
  2541. default:
  2542. return i, false
  2543. case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
  2544. case 'u':
  2545. for j := 0; j < 4; j++ {
  2546. i++
  2547. if i >= len(data) {
  2548. return i, false
  2549. }
  2550. if !((data[i] >= '0' && data[i] <= '9') ||
  2551. (data[i] >= 'a' && data[i] <= 'f') ||
  2552. (data[i] >= 'A' && data[i] <= 'F')) {
  2553. return i, false
  2554. }
  2555. }
  2556. }
  2557. } else if data[i] == '"' {
  2558. return i + 1, true
  2559. }
  2560. }
  2561. return i, false
  2562. }
  2563. func validnumber(data []byte, i int) (outi int, ok bool) {
  2564. i--
  2565. // sign
  2566. if data[i] == '-' {
  2567. i++
  2568. if i == len(data) {
  2569. return i, false
  2570. }
  2571. if data[i] < '0' || data[i] > '9' {
  2572. return i, false
  2573. }
  2574. }
  2575. // int
  2576. if i == len(data) {
  2577. return i, false
  2578. }
  2579. if data[i] == '0' {
  2580. i++
  2581. } else {
  2582. for ; i < len(data); i++ {
  2583. if data[i] >= '0' && data[i] <= '9' {
  2584. continue
  2585. }
  2586. break
  2587. }
  2588. }
  2589. // frac
  2590. if i == len(data) {
  2591. return i, true
  2592. }
  2593. if data[i] == '.' {
  2594. i++
  2595. if i == len(data) {
  2596. return i, false
  2597. }
  2598. if data[i] < '0' || data[i] > '9' {
  2599. return i, false
  2600. }
  2601. i++
  2602. for ; i < len(data); i++ {
  2603. if data[i] >= '0' && data[i] <= '9' {
  2604. continue
  2605. }
  2606. break
  2607. }
  2608. }
  2609. // exp
  2610. if i == len(data) {
  2611. return i, true
  2612. }
  2613. if data[i] == 'e' || data[i] == 'E' {
  2614. i++
  2615. if i == len(data) {
  2616. return i, false
  2617. }
  2618. if data[i] == '+' || data[i] == '-' {
  2619. i++
  2620. }
  2621. if i == len(data) {
  2622. return i, false
  2623. }
  2624. if data[i] < '0' || data[i] > '9' {
  2625. return i, false
  2626. }
  2627. i++
  2628. for ; i < len(data); i++ {
  2629. if data[i] >= '0' && data[i] <= '9' {
  2630. continue
  2631. }
  2632. break
  2633. }
  2634. }
  2635. return i, true
  2636. }
  2637. func validtrue(data []byte, i int) (outi int, ok bool) {
  2638. if i+3 <= len(data) && data[i] == 'r' && data[i+1] == 'u' &&
  2639. data[i+2] == 'e' {
  2640. return i + 3, true
  2641. }
  2642. return i, false
  2643. }
  2644. func validfalse(data []byte, i int) (outi int, ok bool) {
  2645. if i+4 <= len(data) && data[i] == 'a' && data[i+1] == 'l' &&
  2646. data[i+2] == 's' && data[i+3] == 'e' {
  2647. return i + 4, true
  2648. }
  2649. return i, false
  2650. }
  2651. func validnull(data []byte, i int) (outi int, ok bool) {
  2652. if i+3 <= len(data) && data[i] == 'u' && data[i+1] == 'l' &&
  2653. data[i+2] == 'l' {
  2654. return i + 3, true
  2655. }
  2656. return i, false
  2657. }
  2658. // Valid returns true if the input is valid json.
  2659. //
  2660. // if !gjson.Valid(json) {
  2661. // return errors.New("invalid json")
  2662. // }
  2663. // value := gjson.Get(json, "name.last")
  2664. func Valid(json string) bool {
  2665. _, ok := validpayload(stringBytes(json), 0)
  2666. return ok
  2667. }
  2668. // ValidBytes returns true if the input is valid json.
  2669. //
  2670. // if !gjson.Valid(json) {
  2671. // return errors.New("invalid json")
  2672. // }
  2673. // value := gjson.Get(json, "name.last")
  2674. //
  2675. // If working with bytes, this method preferred over ValidBytes(string(data))
  2676. func ValidBytes(json []byte) bool {
  2677. _, ok := validpayload(json, 0)
  2678. return ok
  2679. }
  2680. func parseUint(s string) (n uint64, ok bool) {
  2681. var i int
  2682. if i == len(s) {
  2683. return 0, false
  2684. }
  2685. for ; i < len(s); i++ {
  2686. if s[i] >= '0' && s[i] <= '9' {
  2687. n = n*10 + uint64(s[i]-'0')
  2688. } else {
  2689. return 0, false
  2690. }
  2691. }
  2692. return n, true
  2693. }
  2694. func parseInt(s string) (n int64, ok bool) {
  2695. var i int
  2696. var sign bool
  2697. if len(s) > 0 && s[0] == '-' {
  2698. sign = true
  2699. i++
  2700. }
  2701. if i == len(s) {
  2702. return 0, false
  2703. }
  2704. for ; i < len(s); i++ {
  2705. if s[i] >= '0' && s[i] <= '9' {
  2706. n = n*10 + int64(s[i]-'0')
  2707. } else {
  2708. return 0, false
  2709. }
  2710. }
  2711. if sign {
  2712. return n * -1, true
  2713. }
  2714. return n, true
  2715. }
  2716. // safeInt validates a given JSON number
  2717. // ensures it lies within the minimum and maximum representable JSON numbers
  2718. func safeInt(f float64) (n int64, ok bool) {
  2719. // https://tc39.es/ecma262/#sec-number.min_safe_integer
  2720. // https://tc39.es/ecma262/#sec-number.max_safe_integer
  2721. if f < -9007199254740991 || f > 9007199254740991 {
  2722. return 0, false
  2723. }
  2724. return int64(f), true
  2725. }
  2726. // execStatic parses the path to find a static value.
  2727. // The input expects that the path already starts with a '!'
  2728. func execStatic(json, path string) (pathOut, res string, ok bool) {
  2729. name := path[1:]
  2730. if len(name) > 0 {
  2731. switch name[0] {
  2732. case '{', '[', '"', '+', '-', '0', '1', '2', '3', '4', '5', '6', '7',
  2733. '8', '9':
  2734. _, res = parseSquash(name, 0)
  2735. pathOut = name[len(res):]
  2736. return pathOut, res, true
  2737. }
  2738. }
  2739. for i := 1; i < len(path); i++ {
  2740. if path[i] == '|' {
  2741. pathOut = path[i:]
  2742. name = path[1:i]
  2743. break
  2744. }
  2745. if path[i] == '.' {
  2746. pathOut = path[i:]
  2747. name = path[1:i]
  2748. break
  2749. }
  2750. }
  2751. switch strings.ToLower(name) {
  2752. case "true", "false", "null", "nan", "inf":
  2753. return pathOut, name, true
  2754. }
  2755. return pathOut, res, false
  2756. }
  2757. // execModifier parses the path to find a matching modifier function.
  2758. // The input expects that the path already starts with a '@'
  2759. func execModifier(json, path string) (pathOut, res string, ok bool) {
  2760. name := path[1:]
  2761. var hasArgs bool
  2762. for i := 1; i < len(path); i++ {
  2763. if path[i] == ':' {
  2764. pathOut = path[i+1:]
  2765. name = path[1:i]
  2766. hasArgs = len(pathOut) > 0
  2767. break
  2768. }
  2769. if path[i] == '|' {
  2770. pathOut = path[i:]
  2771. name = path[1:i]
  2772. break
  2773. }
  2774. if path[i] == '.' {
  2775. pathOut = path[i:]
  2776. name = path[1:i]
  2777. break
  2778. }
  2779. }
  2780. if fn, ok := modifiers[name]; ok {
  2781. var args string
  2782. if hasArgs {
  2783. var parsedArgs bool
  2784. switch pathOut[0] {
  2785. case '{', '[', '"':
  2786. // json arg
  2787. res := Parse(pathOut)
  2788. if res.Exists() {
  2789. args = squash(pathOut)
  2790. pathOut = pathOut[len(args):]
  2791. parsedArgs = true
  2792. }
  2793. }
  2794. if !parsedArgs {
  2795. // simple arg
  2796. i := 0
  2797. for ; i < len(pathOut); i++ {
  2798. if pathOut[i] == '|' {
  2799. break
  2800. }
  2801. switch pathOut[i] {
  2802. case '{', '[', '"', '(':
  2803. s := squash(pathOut[i:])
  2804. i += len(s) - 1
  2805. }
  2806. }
  2807. args = pathOut[:i]
  2808. pathOut = pathOut[i:]
  2809. }
  2810. }
  2811. return pathOut, fn(json, args), true
  2812. }
  2813. return pathOut, res, false
  2814. }
  2815. // unwrap removes the '[]' or '{}' characters around json
  2816. func unwrap(json string) string {
  2817. json = trim(json)
  2818. if len(json) >= 2 && (json[0] == '[' || json[0] == '{') {
  2819. json = json[1 : len(json)-1]
  2820. }
  2821. return json
  2822. }
  2823. // DisableModifiers will disable the modifier syntax
  2824. var DisableModifiers = false
  2825. var modifiers map[string]func(json, arg string) string
  2826. func init() {
  2827. modifiers = map[string]func(json, arg string) string{
  2828. "pretty": modPretty,
  2829. "ugly": modUgly,
  2830. "reverse": modReverse,
  2831. "this": modThis,
  2832. "flatten": modFlatten,
  2833. "join": modJoin,
  2834. "valid": modValid,
  2835. "keys": modKeys,
  2836. "values": modValues,
  2837. "tostr": modToStr,
  2838. "fromstr": modFromStr,
  2839. "group": modGroup,
  2840. "dig": modDig,
  2841. }
  2842. }
  2843. // AddModifier binds a custom modifier command to the GJSON syntax.
  2844. // This operation is not thread safe and should be executed prior to
  2845. // using all other gjson function.
  2846. func AddModifier(name string, fn func(json, arg string) string) {
  2847. modifiers[name] = fn
  2848. }
  2849. // ModifierExists returns true when the specified modifier exists.
  2850. func ModifierExists(name string, fn func(json, arg string) string) bool {
  2851. _, ok := modifiers[name]
  2852. return ok
  2853. }
  2854. // cleanWS remove any non-whitespace from string
  2855. func cleanWS(s string) string {
  2856. for i := 0; i < len(s); i++ {
  2857. switch s[i] {
  2858. case ' ', '\t', '\n', '\r':
  2859. continue
  2860. default:
  2861. var s2 []byte
  2862. for i := 0; i < len(s); i++ {
  2863. switch s[i] {
  2864. case ' ', '\t', '\n', '\r':
  2865. s2 = append(s2, s[i])
  2866. }
  2867. }
  2868. return string(s2)
  2869. }
  2870. }
  2871. return s
  2872. }
  2873. // @pretty modifier makes the json look nice.
  2874. func modPretty(json, arg string) string {
  2875. if len(arg) > 0 {
  2876. opts := *pretty.DefaultOptions
  2877. Parse(arg).ForEach(func(key, value Result) bool {
  2878. switch key.String() {
  2879. case "sortKeys":
  2880. opts.SortKeys = value.Bool()
  2881. case "indent":
  2882. opts.Indent = cleanWS(value.String())
  2883. case "prefix":
  2884. opts.Prefix = cleanWS(value.String())
  2885. case "width":
  2886. opts.Width = int(value.Int())
  2887. }
  2888. return true
  2889. })
  2890. return bytesString(pretty.PrettyOptions(stringBytes(json), &opts))
  2891. }
  2892. return bytesString(pretty.Pretty(stringBytes(json)))
  2893. }
  2894. // @this returns the current element. Can be used to retrieve the root element.
  2895. func modThis(json, arg string) string {
  2896. return json
  2897. }
  2898. // @ugly modifier removes all whitespace.
  2899. func modUgly(json, arg string) string {
  2900. return bytesString(pretty.Ugly(stringBytes(json)))
  2901. }
  2902. // @reverse reverses array elements or root object members.
  2903. func modReverse(json, arg string) string {
  2904. res := Parse(json)
  2905. if res.IsArray() {
  2906. var values []Result
  2907. res.ForEach(func(_, value Result) bool {
  2908. values = append(values, value)
  2909. return true
  2910. })
  2911. out := make([]byte, 0, len(json))
  2912. out = append(out, '[')
  2913. for i, j := len(values)-1, 0; i >= 0; i, j = i-1, j+1 {
  2914. if j > 0 {
  2915. out = append(out, ',')
  2916. }
  2917. out = append(out, values[i].Raw...)
  2918. }
  2919. out = append(out, ']')
  2920. return bytesString(out)
  2921. }
  2922. if res.IsObject() {
  2923. var keyValues []Result
  2924. res.ForEach(func(key, value Result) bool {
  2925. keyValues = append(keyValues, key, value)
  2926. return true
  2927. })
  2928. out := make([]byte, 0, len(json))
  2929. out = append(out, '{')
  2930. for i, j := len(keyValues)-2, 0; i >= 0; i, j = i-2, j+1 {
  2931. if j > 0 {
  2932. out = append(out, ',')
  2933. }
  2934. out = append(out, keyValues[i+0].Raw...)
  2935. out = append(out, ':')
  2936. out = append(out, keyValues[i+1].Raw...)
  2937. }
  2938. out = append(out, '}')
  2939. return bytesString(out)
  2940. }
  2941. return json
  2942. }
  2943. // @flatten an array with child arrays.
  2944. //
  2945. // [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]]
  2946. //
  2947. // The {"deep":true} arg can be provide for deep flattening.
  2948. //
  2949. // [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7]
  2950. //
  2951. // The original json is returned when the json is not an array.
  2952. func modFlatten(json, arg string) string {
  2953. res := Parse(json)
  2954. if !res.IsArray() {
  2955. return json
  2956. }
  2957. var deep bool
  2958. if arg != "" {
  2959. Parse(arg).ForEach(func(key, value Result) bool {
  2960. if key.String() == "deep" {
  2961. deep = value.Bool()
  2962. }
  2963. return true
  2964. })
  2965. }
  2966. var out []byte
  2967. out = append(out, '[')
  2968. var idx int
  2969. res.ForEach(func(_, value Result) bool {
  2970. var raw string
  2971. if value.IsArray() {
  2972. if deep {
  2973. raw = unwrap(modFlatten(value.Raw, arg))
  2974. } else {
  2975. raw = unwrap(value.Raw)
  2976. }
  2977. } else {
  2978. raw = value.Raw
  2979. }
  2980. raw = strings.TrimSpace(raw)
  2981. if len(raw) > 0 {
  2982. if idx > 0 {
  2983. out = append(out, ',')
  2984. }
  2985. out = append(out, raw...)
  2986. idx++
  2987. }
  2988. return true
  2989. })
  2990. out = append(out, ']')
  2991. return bytesString(out)
  2992. }
  2993. // @keys extracts the keys from an object.
  2994. //
  2995. // {"first":"Tom","last":"Smith"} -> ["first","last"]
  2996. func modKeys(json, arg string) string {
  2997. v := Parse(json)
  2998. if !v.Exists() {
  2999. return "[]"
  3000. }
  3001. obj := v.IsObject()
  3002. var out strings.Builder
  3003. out.WriteByte('[')
  3004. var i int
  3005. v.ForEach(func(key, _ Result) bool {
  3006. if i > 0 {
  3007. out.WriteByte(',')
  3008. }
  3009. if obj {
  3010. out.WriteString(key.Raw)
  3011. } else {
  3012. out.WriteString("null")
  3013. }
  3014. i++
  3015. return true
  3016. })
  3017. out.WriteByte(']')
  3018. return out.String()
  3019. }
  3020. // @values extracts the values from an object.
  3021. //
  3022. // {"first":"Tom","last":"Smith"} -> ["Tom","Smith"]
  3023. func modValues(json, arg string) string {
  3024. v := Parse(json)
  3025. if !v.Exists() {
  3026. return "[]"
  3027. }
  3028. if v.IsArray() {
  3029. return json
  3030. }
  3031. var out strings.Builder
  3032. out.WriteByte('[')
  3033. var i int
  3034. v.ForEach(func(_, value Result) bool {
  3035. if i > 0 {
  3036. out.WriteByte(',')
  3037. }
  3038. out.WriteString(value.Raw)
  3039. i++
  3040. return true
  3041. })
  3042. out.WriteByte(']')
  3043. return out.String()
  3044. }
  3045. // @join multiple objects into a single object.
  3046. //
  3047. // [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"}
  3048. //
  3049. // The arg can be "true" to specify that duplicate keys should be preserved.
  3050. //
  3051. // [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41}
  3052. //
  3053. // Without preserved keys:
  3054. //
  3055. // [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41}
  3056. //
  3057. // The original json is returned when the json is not an object.
  3058. func modJoin(json, arg string) string {
  3059. res := Parse(json)
  3060. if !res.IsArray() {
  3061. return json
  3062. }
  3063. var preserve bool
  3064. if arg != "" {
  3065. Parse(arg).ForEach(func(key, value Result) bool {
  3066. if key.String() == "preserve" {
  3067. preserve = value.Bool()
  3068. }
  3069. return true
  3070. })
  3071. }
  3072. var out []byte
  3073. out = append(out, '{')
  3074. if preserve {
  3075. // Preserve duplicate keys.
  3076. var idx int
  3077. res.ForEach(func(_, value Result) bool {
  3078. if !value.IsObject() {
  3079. return true
  3080. }
  3081. if idx > 0 {
  3082. out = append(out, ',')
  3083. }
  3084. out = append(out, unwrap(value.Raw)...)
  3085. idx++
  3086. return true
  3087. })
  3088. } else {
  3089. // Deduplicate keys and generate an object with stable ordering.
  3090. var keys []Result
  3091. kvals := make(map[string]Result)
  3092. res.ForEach(func(_, value Result) bool {
  3093. if !value.IsObject() {
  3094. return true
  3095. }
  3096. value.ForEach(func(key, value Result) bool {
  3097. k := key.String()
  3098. if _, ok := kvals[k]; !ok {
  3099. keys = append(keys, key)
  3100. }
  3101. kvals[k] = value
  3102. return true
  3103. })
  3104. return true
  3105. })
  3106. for i := 0; i < len(keys); i++ {
  3107. if i > 0 {
  3108. out = append(out, ',')
  3109. }
  3110. out = append(out, keys[i].Raw...)
  3111. out = append(out, ':')
  3112. out = append(out, kvals[keys[i].String()].Raw...)
  3113. }
  3114. }
  3115. out = append(out, '}')
  3116. return bytesString(out)
  3117. }
  3118. // @valid ensures that the json is valid before moving on. An empty string is
  3119. // returned when the json is not valid, otherwise it returns the original json.
  3120. func modValid(json, arg string) string {
  3121. if !Valid(json) {
  3122. return ""
  3123. }
  3124. return json
  3125. }
  3126. // @fromstr converts a string to json
  3127. //
  3128. // "{\"id\":1023,\"name\":\"alert\"}" -> {"id":1023,"name":"alert"}
  3129. func modFromStr(json, arg string) string {
  3130. if !Valid(json) {
  3131. return ""
  3132. }
  3133. return Parse(json).String()
  3134. }
  3135. // @tostr converts a string to json
  3136. //
  3137. // {"id":1023,"name":"alert"} -> "{\"id\":1023,\"name\":\"alert\"}"
  3138. func modToStr(str, arg string) string {
  3139. return string(AppendJSONString(nil, str))
  3140. }
  3141. func modGroup(json, arg string) string {
  3142. res := Parse(json)
  3143. if !res.IsObject() {
  3144. return ""
  3145. }
  3146. var all [][]byte
  3147. res.ForEach(func(key, value Result) bool {
  3148. if !value.IsArray() {
  3149. return true
  3150. }
  3151. var idx int
  3152. value.ForEach(func(_, value Result) bool {
  3153. if idx == len(all) {
  3154. all = append(all, []byte{})
  3155. }
  3156. all[idx] = append(all[idx], ("," + key.Raw + ":" + value.Raw)...)
  3157. idx++
  3158. return true
  3159. })
  3160. return true
  3161. })
  3162. var data []byte
  3163. data = append(data, '[')
  3164. for i, item := range all {
  3165. if i > 0 {
  3166. data = append(data, ',')
  3167. }
  3168. data = append(data, '{')
  3169. data = append(data, item[1:]...)
  3170. data = append(data, '}')
  3171. }
  3172. data = append(data, ']')
  3173. return string(data)
  3174. }
  3175. // stringHeader instead of reflect.StringHeader
  3176. type stringHeader struct {
  3177. data unsafe.Pointer
  3178. len int
  3179. }
  3180. // sliceHeader instead of reflect.SliceHeader
  3181. type sliceHeader struct {
  3182. data unsafe.Pointer
  3183. len int
  3184. cap int
  3185. }
  3186. // getBytes casts the input json bytes to a string and safely returns the
  3187. // results as uniquely allocated data. This operation is intended to minimize
  3188. // copies and allocations for the large json string->[]byte.
  3189. func getBytes(json []byte, path string) Result {
  3190. var result Result
  3191. if json != nil {
  3192. // unsafe cast to string
  3193. result = Get(*(*string)(unsafe.Pointer(&json)), path)
  3194. // safely get the string headers
  3195. rawhi := *(*stringHeader)(unsafe.Pointer(&result.Raw))
  3196. strhi := *(*stringHeader)(unsafe.Pointer(&result.Str))
  3197. // create byte slice headers
  3198. rawh := sliceHeader{data: rawhi.data, len: rawhi.len, cap: rawhi.len}
  3199. strh := sliceHeader{data: strhi.data, len: strhi.len, cap: rawhi.len}
  3200. if strh.data == nil {
  3201. // str is nil
  3202. if rawh.data == nil {
  3203. // raw is nil
  3204. result.Raw = ""
  3205. } else {
  3206. // raw has data, safely copy the slice header to a string
  3207. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3208. }
  3209. result.Str = ""
  3210. } else if rawh.data == nil {
  3211. // raw is nil
  3212. result.Raw = ""
  3213. // str has data, safely copy the slice header to a string
  3214. result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
  3215. } else if uintptr(strh.data) >= uintptr(rawh.data) &&
  3216. uintptr(strh.data)+uintptr(strh.len) <=
  3217. uintptr(rawh.data)+uintptr(rawh.len) {
  3218. // Str is a substring of Raw.
  3219. start := uintptr(strh.data) - uintptr(rawh.data)
  3220. // safely copy the raw slice header
  3221. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3222. // substring the raw
  3223. result.Str = result.Raw[start : start+uintptr(strh.len)]
  3224. } else {
  3225. // safely copy both the raw and str slice headers to strings
  3226. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3227. result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
  3228. }
  3229. }
  3230. return result
  3231. }
  3232. // fillIndex finds the position of Raw data and assigns it to the Index field
  3233. // of the resulting value. If the position cannot be found then Index zero is
  3234. // used instead.
  3235. func fillIndex(json string, c *parseContext) {
  3236. if len(c.value.Raw) > 0 && !c.calcd {
  3237. jhdr := *(*stringHeader)(unsafe.Pointer(&json))
  3238. rhdr := *(*stringHeader)(unsafe.Pointer(&(c.value.Raw)))
  3239. c.value.Index = int(uintptr(rhdr.data) - uintptr(jhdr.data))
  3240. if c.value.Index < 0 || c.value.Index >= len(json) {
  3241. c.value.Index = 0
  3242. }
  3243. }
  3244. }
  3245. func stringBytes(s string) []byte {
  3246. return *(*[]byte)(unsafe.Pointer(&sliceHeader{
  3247. data: (*stringHeader)(unsafe.Pointer(&s)).data,
  3248. len: len(s),
  3249. cap: len(s),
  3250. }))
  3251. }
  3252. func bytesString(b []byte) string {
  3253. return *(*string)(unsafe.Pointer(&b))
  3254. }
  3255. func revSquash(json string) string {
  3256. // reverse squash
  3257. // expects that the tail character is a ']' or '}' or ')' or '"'
  3258. // squash the value, ignoring all nested arrays and objects.
  3259. i := len(json) - 1
  3260. var depth int
  3261. if json[i] != '"' {
  3262. depth++
  3263. }
  3264. if json[i] == '}' || json[i] == ']' || json[i] == ')' {
  3265. i--
  3266. }
  3267. for ; i >= 0; i-- {
  3268. switch json[i] {
  3269. case '"':
  3270. i--
  3271. for ; i >= 0; i-- {
  3272. if json[i] == '"' {
  3273. esc := 0
  3274. for i > 0 && json[i-1] == '\\' {
  3275. i--
  3276. esc++
  3277. }
  3278. if esc%2 == 1 {
  3279. continue
  3280. }
  3281. i += esc
  3282. break
  3283. }
  3284. }
  3285. if depth == 0 {
  3286. if i < 0 {
  3287. i = 0
  3288. }
  3289. return json[i:]
  3290. }
  3291. case '}', ']', ')':
  3292. depth++
  3293. case '{', '[', '(':
  3294. depth--
  3295. if depth == 0 {
  3296. return json[i:]
  3297. }
  3298. }
  3299. }
  3300. return json
  3301. }
  3302. // Paths returns the original GJSON paths for a Result where the Result came
  3303. // from a simple query path that returns an array, like:
  3304. //
  3305. // gjson.Get(json, "friends.#.first")
  3306. //
  3307. // The returned value will be in the form of a JSON array:
  3308. //
  3309. // ["friends.0.first","friends.1.first","friends.2.first"]
  3310. //
  3311. // The param 'json' must be the original JSON used when calling Get.
  3312. //
  3313. // Returns an empty string if the paths cannot be determined, which can happen
  3314. // when the Result came from a path that contained a multipath, modifier,
  3315. // or a nested query.
  3316. func (t Result) Paths(json string) []string {
  3317. if t.Indexes == nil {
  3318. return nil
  3319. }
  3320. paths := make([]string, 0, len(t.Indexes))
  3321. t.ForEach(func(_, value Result) bool {
  3322. paths = append(paths, value.Path(json))
  3323. return true
  3324. })
  3325. if len(paths) != len(t.Indexes) {
  3326. return nil
  3327. }
  3328. return paths
  3329. }
  3330. // Path returns the original GJSON path for a Result where the Result came
  3331. // from a simple path that returns a single value, like:
  3332. //
  3333. // gjson.Get(json, "friends.#(last=Murphy)")
  3334. //
  3335. // The returned value will be in the form of a JSON string:
  3336. //
  3337. // "friends.0"
  3338. //
  3339. // The param 'json' must be the original JSON used when calling Get.
  3340. //
  3341. // Returns an empty string if the paths cannot be determined, which can happen
  3342. // when the Result came from a path that contained a multipath, modifier,
  3343. // or a nested query.
  3344. func (t Result) Path(json string) string {
  3345. var path []byte
  3346. var comps []string // raw components
  3347. i := t.Index - 1
  3348. if t.Index+len(t.Raw) > len(json) {
  3349. // JSON cannot safely contain Result.
  3350. goto fail
  3351. }
  3352. if !strings.HasPrefix(json[t.Index:], t.Raw) {
  3353. // Result is not at the JSON index as expected.
  3354. goto fail
  3355. }
  3356. for ; i >= 0; i-- {
  3357. if json[i] <= ' ' {
  3358. continue
  3359. }
  3360. if json[i] == ':' {
  3361. // inside of object, get the key
  3362. for ; i >= 0; i-- {
  3363. if json[i] != '"' {
  3364. continue
  3365. }
  3366. break
  3367. }
  3368. raw := revSquash(json[:i+1])
  3369. i = i - len(raw)
  3370. comps = append(comps, raw)
  3371. // key gotten, now squash the rest
  3372. raw = revSquash(json[:i+1])
  3373. i = i - len(raw)
  3374. i++ // increment the index for next loop step
  3375. } else if json[i] == '{' {
  3376. // Encountered an open object. The original result was probably an
  3377. // object key.
  3378. goto fail
  3379. } else if json[i] == ',' || json[i] == '[' {
  3380. // inside of an array, count the position
  3381. var arrIdx int
  3382. if json[i] == ',' {
  3383. arrIdx++
  3384. i--
  3385. }
  3386. for ; i >= 0; i-- {
  3387. if json[i] == ':' {
  3388. // Encountered an unexpected colon. The original result was
  3389. // probably an object key.
  3390. goto fail
  3391. } else if json[i] == ',' {
  3392. arrIdx++
  3393. } else if json[i] == '[' {
  3394. comps = append(comps, strconv.Itoa(arrIdx))
  3395. break
  3396. } else if json[i] == ']' || json[i] == '}' || json[i] == '"' {
  3397. raw := revSquash(json[:i+1])
  3398. i = i - len(raw) + 1
  3399. }
  3400. }
  3401. }
  3402. }
  3403. if len(comps) == 0 {
  3404. if DisableModifiers {
  3405. goto fail
  3406. }
  3407. return "@this"
  3408. }
  3409. for i := len(comps) - 1; i >= 0; i-- {
  3410. rcomp := Parse(comps[i])
  3411. if !rcomp.Exists() {
  3412. goto fail
  3413. }
  3414. comp := Escape(rcomp.String())
  3415. path = append(path, '.')
  3416. path = append(path, comp...)
  3417. }
  3418. if len(path) > 0 {
  3419. path = path[1:]
  3420. }
  3421. return string(path)
  3422. fail:
  3423. return ""
  3424. }
  3425. // isSafePathKeyChar returns true if the input character is safe for not
  3426. // needing escaping.
  3427. func isSafePathKeyChar(c byte) bool {
  3428. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  3429. (c >= '0' && c <= '9') || c <= ' ' || c > '~' || c == '_' ||
  3430. c == '-' || c == ':'
  3431. }
  3432. // Escape returns an escaped path component.
  3433. //
  3434. // json := `{
  3435. // "user":{
  3436. // "first.name": "Janet",
  3437. // "last.name": "Prichard"
  3438. // }
  3439. // }`
  3440. // user := gjson.Get(json, "user")
  3441. // println(user.Get(gjson.Escape("first.name"))
  3442. // println(user.Get(gjson.Escape("last.name"))
  3443. // // Output:
  3444. // // Janet
  3445. // // Prichard
  3446. func Escape(comp string) string {
  3447. for i := 0; i < len(comp); i++ {
  3448. if !isSafePathKeyChar(comp[i]) {
  3449. ncomp := make([]byte, len(comp)+1)
  3450. copy(ncomp, comp[:i])
  3451. ncomp = ncomp[:i]
  3452. for ; i < len(comp); i++ {
  3453. if !isSafePathKeyChar(comp[i]) {
  3454. ncomp = append(ncomp, '\\')
  3455. }
  3456. ncomp = append(ncomp, comp[i])
  3457. }
  3458. return string(ncomp)
  3459. }
  3460. }
  3461. return comp
  3462. }
  3463. func parseRecursiveDescent(all []Result, parent Result, path string) []Result {
  3464. if res := parent.Get(path); res.Exists() {
  3465. all = append(all, res)
  3466. }
  3467. if parent.IsArray() || parent.IsObject() {
  3468. parent.ForEach(func(_, val Result) bool {
  3469. all = parseRecursiveDescent(all, val, path)
  3470. return true
  3471. })
  3472. }
  3473. return all
  3474. }
  3475. func modDig(json, arg string) string {
  3476. all := parseRecursiveDescent(nil, Parse(json), arg)
  3477. var out []byte
  3478. out = append(out, '[')
  3479. for i, res := range all {
  3480. if i > 0 {
  3481. out = append(out, ',')
  3482. }
  3483. out = append(out, res.Raw...)
  3484. }
  3485. out = append(out, ']')
  3486. return string(out)
  3487. }