\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Jianmin Feng (taotao)} \pdfinfo{ /Title (first-scope-of-json.pdf) /Creator (Cheatography) /Author (Jianmin Feng (taotao)) /Subject (First Scope of Json Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{3C592F} \definecolor{LightBackground}{HTML}{F8F9F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{First Scope of Json Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jianmin Feng (taotao)} via \textcolor{DarkBackground}{\uline{cheatography.com/79308/cs/19247/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Jianmin Feng (taotao) \\ \uline{cheatography.com/taotao} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 22nd April, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{What's JSON?}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{J}}ava{\bf{S}}cript {\bf{O}}bject {\bf{N}}otation} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Language independent data format for storing and sharing} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Lightweight,human readable,easy parsed/generated} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Name is misleading somehow} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{JSON vs XML}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Shorter,more lightweight} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Wirteable, more readable} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{More easier to parse by programming} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Easier to work with arrays} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{No namespace, comments not allowed} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{XML like a truck, suitable for large scale file based data exchange, while JSON like a compact car, good for small flexible data exchange over network, such as ajax} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Examples XML vs JSON}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textless{}departments\textgreater{} \newline \textless{}department\textgreater{} \newline \textless{}dept\_id\textgreater{}1 \textless{}/dept\_id\textgreater{} \newline \textless{}manager\textgreater{}Josh\textless{}/manager\textgreater{} \newline \textless{}employees\textgreater{} \newline \textless{}employee\textgreater{} \newline \textless{}id\textgreater{}7839\textless{}/id\textgreater{} \newline \textless{}name\textgreater{}John\textless{}/name\textgreater{} \newline \textless{}jobs\textgreater{} \newline \textless{}job\textgreater{} \newline \textless{}Title\textgreater{}Clerk\textless{}/Tile\textgreater{} \newline \textless{}/job\textgreater{} \newline \textless{}job\textgreater{} \newline \textless{}Title\textgreater{}dba\textless{}/Tile\textgreater{} \newline \textless{}/job\textgreater{} \newline \textless{}/jobs\textgreater{} \newline \textless{}/employee\textgreater{} \newline \textless{}employee\textgreater{} \newline \textless{}id\textgreater{}7698\textless{}/id\textgreater{} \newline \textless{}name\textgreater{}Tom\textless{}/name\textgreater{} \newline \textless{}jobs\textgreater{} \newline \textless{}job\textgreater{} \newline \textless{}Title\textgreater{}Clerk\textless{}/Tile\textgreater{} \newline \textless{}/job\textgreater{} \newline \textless{}job\textgreater{} \newline \textless{}Title\textgreater{}Analyst\textless{}/Tile\textgreater{} \newline \textless{}/job\textgreater{} \newline \textless{}/jobs\textgreater{} \newline \textless{}/employee\textgreater{} \newline \textless{}/employees\textgreater{} \newline \textless{}/department\textgreater{} \newline \textless{}/departments\textgreater{} \newline \newline \{departments:{[}\{ \newline "department":\{ \newline "dept\_id":"1", \newline "employees":{[}\{ \newline "employee":\{ \newline "id":66", \newline "name":"John", \newline \{jobs:{[}"Clerk","dba"{]}\}\}, \newline "employee":\{ \newline "id":88, \newline "name":"Tom", \newline \{jobs:{[}"Analyst"{]}\}\} \newline {]}\} \newline \}{]}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.64701 cm} x{4.32999 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{JSON Structure}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Object} & \{string1:value1,string2:value2,...\} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} array & {[}value1,value2,...{]} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} value & string,number,true/false,null,object,array \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Oracle JSON Support}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{12.1.0.2} & store in varchar2,clob,blob w/ check constraint \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Apex 5.0 & Apex\_JSON \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{pljson} & github.com/pljson/pljson \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{NoSql - RDBMS - blender with JSON support} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Load JSON By External Table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{CREATE TABLE JSON\_DEMO( id number, json\_data clob) \newline ORGANIZATION EXTERNAL \newline ( TYPE ORACLE\_LOADER \newline DEFAULT DIRECTORY EXT\_DIR \newline ACCESS PARAMETERS \newline ( records delimited by newline \newline fields terminated by 0X'09' \newline missing field values are null \newline ( id,fname ) \newline COLUMN TRANSFORMS ( \newline json\_data FROM LOBFILE(fname) FROM (EXT\_DIR) ) \newline ) \newline LOCATION \seqsplit{(EXT\_DIR:'json\_file\_list\_to\_be\_load}.txt') \newline ) REJECT LIMIT UNLIMITED;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Content in \seqsplit{json\_file\_list\_to\_be\_load}.txt \newline 1 jason\_data.json} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Parse with APEX\_JSON}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1 select json\_clob into v\_json\_data from json\_demo where id=1;} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{2 \seqsplit{APEX\_JSON.parse(v\_json\_data} ); -{}- g\_value variable hold the data} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{3 \seqsplit{APEX\_JSON.get\_varchar2(v\_json\_data},'dept');} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{4. \seqsplit{APEX\_JSON.get\_count(v\_json\_data},'dept.emp');} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{5 loop based on count} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{page break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle JSON Dot Notation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{select jd.json\_doc.departments{[}0{]}.employees{[}0{]}.name from json\_demo jd; \newline \newline -{}-dot \seqsplit{notation:table\_alias.json\_col.key1}.key2{[}0,1..{]}, \newline -{}-table must be aliased \newline -{}-col must be checked IS JSON, no need to enable \newline -{}-key is case-senstive} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{dot notion is slower and easily confused, use json\_table instead.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle 12.1.0.2 JSON Condition}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. IS JSON \newline create table json\_demo \newline ( \newline json\_doc clob not null, \newline ts date default sysdate \newline ); \newline \newline alter table json\_demo \newline add constriant ck\_is\_json \newline check (json\_doc IS JSON WITH UNIQUE KEYS (STRICT)); \newline \newline -{}-WITH UNIQUE KEYS: use care (performance) \newline -{}-(STRICT): force strict over Lax syntax, such as case sensitive," not ' etc \newline -{}-ORA-02290: check constraint (TEST.DOCUMENT\_JSON) violated \newline \newline 2. JSON\_EXISTS(json\_doc,'\$.employee\_no' FALSE ON ERROR);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{JSON doc saved to VARCHAR2, CLOB or BLOB with conditions} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle JSON Path}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$.Title.Songs{[}*{]}.producer{[}0{]}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{array index could be * or ordered index, no duplicate} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle JSON\_VALUE: retrieve scalar value}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{select JSON\_VALUE(json\_data, '\$.Title.Songs{[}{]}.producer{[}0{]}.',RETURNING VARCHAR2,ERROR ON ERROR ) \newline from json\_demo; \newline \newline -{}-Mandatory: table \& Col name, add FORMAT JASON for BLOB \newline -{}-Optional: Returning type - varchar2, number,date, timestamp, 18c suppoert CLOB,BLOB \newline -{}-Optional: error NULL ON ERROR(default), ERROR ON ERROR} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle JSON\_QUERY: wrap return into array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{select JSON\_QUERY(json\_data, '\$.Songs{[}0{]}.producer{[}*{]}.Name WITH WRAPPER) from json\_demo; \newline \newline -{}-return type: only varchar2 \newline -{}-WITH WRAPPER: default WITOUT WRAPPER \newline -{}-EMPTY ON ERROR(default)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Oracle JASON\_TABLE:JSON doc -{}-\textgreater{} table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{select t.{\emph{ \newline from json\_demo, \newline JSON\_TABLE ( json\_doc, '\$.departments{[}}}{]}' \newline COLUMNS ( \newline row\_number FOR ORDINALITY, \newline song varchar2(50) PATH '\$.Title', \newline artist varchar2(50) PATH '\$.Artist, \newline ESTED PATH '\$.producer{[}*{]}' \newline columns( \newline company varchar2(50) path '\$.company' \newline ) \newline )) \newline AS t; \newline \newline -{}-original table must included in from \newline -{}-Column path is from table path \newline -{}-FOR CARDINALITY: get row number} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{NESTED PATH only available for 12.2 above} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{FORMAT JSON}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{implict for all json column \newline % Row Count 1 (+ 1) explictly specify if column type is BLOB% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\{USER|ALL|DBA\}\_JSON\_COLUMNS Views}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{SELECT table\_name,column\_name,format,data\_type \newline % Row Count 1 (+ 1) FROM user\_json\_columns;% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{page break (copy)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{JSON\_TEXTCONTAINS}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. context search index(full text search or json search idnex \newline -{}-12.1 \newline CREATE INDEX json\_docs\_search\_idx ON json\_doc(data) \newline INDEXTYPE IS CTXSYS.CONTEXT \newline PARAMETERS ('section group \seqsplit{CTXSYS.JSON\_SECTION\_GROUP} SYNC (ON COMMIT)'); \newline \newline -{}-12.2 \newline CREATE SEARCH INDEX json\_docs\_search\_idx ON json\_doc(data) FOR JSON; \newline \newline EXEC \seqsplit{DBMS\_STATS.gather\_table\_stats(USER}, 'JSON\_DOCUMENTS'); \newline \newline 2. query \newline SELECT COUNT({\emph{) FROM json\_doc \newline WHERE JSON\_TEXTCONTAINS(data, \seqsplit{'\$.ContactDetails.Email'}, 'john.doe@example.com'); \newline \newline SELECT COUNT(}}) FROM json\_documents \newline WHERE JSON\_EXISTS(data, '\$.ContactDetails');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}