snipt

Ctrl+h for KB shortcuts

Java

Write arbitrary object as JSON

1
2
3
4
5
6
7
String json = "";
try {
    ObjectWriter ow = new ObjectMapper().writer();
    json = ow.writeValueAsString(myArbitraryObject);
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
}
https://snipt.net/embed/02ba9ad31c1a57eab7a3374f854c2242/
https://snipt.net/raw/02ba9ad31c1a57eab7a3374f854c2242/
02ba9ad31c1a57eab7a3374f854c2242
java
Java
7
2016-09-30T06:48:51
True
False
False
/api/public/snipt/137714/
write-arbitrary-object-as-json-982a6d99
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">String</span> <span class="n">json</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="o">;</span> </span><span id="L-2"><a name="L-2"></a><span class="k">try</span> <span class="o">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">ObjectWriter</span> <span class="n">ow</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ObjectMapper</span><span class="o">().</span><span class="na">writer</span><span class="o">();</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">json</span> <span class="o">=</span> <span class="n">ow</span><span class="o">.</span><span class="na">writeValueAsString</span><span class="o">(</span><span class="n">myArbitraryObject</span><span class="o">);</span> </span><span id="L-5"><a name="L-5"></a><span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">(),</span> <span class="n">e</span><span class="o">);</span> </span><span id="L-7"><a name="L-7"></a><span class="o">}</span> </span></pre></div> </td></tr></table>
JSON, Jackson, Java, JavaScript, object2json

PHP

Parse JSON to Array

1
2
3
4
5
6
7
function parse_json_to_array() {	
	$json_string = file_get_contents($url);
	if($json_string === FALSE) return false;

	$json_array  = json_decode($json_string,true);
	return $json_array;
}
https://snipt.net/embed/f322b79c9b1cf89ea2a42867004ef6de/
https://snipt.net/raw/f322b79c9b1cf89ea2a42867004ef6de/
f322b79c9b1cf89ea2a42867004ef6de
php
PHP
7
2016-10-01T12:18:05
True
False
False
Dec 18, 2013 at 01:22 AM
/api/public/snipt/101198/
parse-json-to-array
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">function parse_json_to_array() { </span> </span><span id="L-2"><a name="L-2"></a><span class="x"> $json_string = file_get_contents($url);</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> if($json_string === FALSE) return false;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="x"> $json_array = json_decode($json_string,true);</span> </span><span id="L-6"><a name="L-6"></a><span class="x"> return $json_array;</span> </span><span id="L-7"><a name="L-7"></a><span class="x">}</span> </span></pre></div> </td></tr></table>
"Parse JSON", JSON

Bash

Pretty print JSON in the command line with python

$ curl https://github.com/username.json | python -mjson.tool
https://snipt.net/embed/12d2ed0e8a67a7735eeda19650762fc6/
https://snipt.net/raw/12d2ed0e8a67a7735eeda19650762fc6/
12d2ed0e8a67a7735eeda19650762fc6
bash
Bash
1
2016-10-01T12:38:55
True
False
False
Jan 21, 2013 at 03:09 AM
/api/public/snipt/54114/
pretty-print-json-in-the-command-line-with-python
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">$ </span>curl https://github.com/username.json <span class="p">|</span> python -mjson.tool </span></pre></div> </td></tr></table>
JSON, bash, python

JavaScript

Parse JSON with Mustache and JQuery

1
2
3
4
5
$.getJSON('json/data.json', function(data) {
    var template = "<h1>{{firstName}} {{lastName}}</h1>Blog: {{blogURL}}";
    var html = Mustache.to_html(template, data);
    $('#sampleArea').html(html);
});
https://snipt.net/embed/d1ba02e32dcb8fd8d048e8366016a60d/
https://snipt.net/raw/d1ba02e32dcb8fd8d048e8366016a60d/
d1ba02e32dcb8fd8d048e8366016a60d
js
JavaScript
5
2016-10-01T06:35:04
True
False
False
/api/public/snipt/50029/
parse-json-with-mustache-and-jquery
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">.</span><span class="nx">getJSON</span><span class="p">(</span><span class="s1">&#39;json/data.json&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">template</span> <span class="o">=</span> <span class="s2">&quot;&lt;h1&gt;{{firstName}} {{lastName}}&lt;/h1&gt;Blog: {{blogURL}}&quot;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">Mustache</span><span class="p">.</span><span class="nx">to_html</span><span class="p">(</span><span class="nx">template</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#sampleArea&#39;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
Ajax, JQuery, JSON, Mustache
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.