Querying JSON

From zero to hero

Gabriel Trujillo C.

@TheDull

Serialization

"The process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment."

(https://en.wikipedia.org/wiki/Serialization)

(Marshalling)

Serialization: How?

  • 1980-1987: XNS, XDR
  • 1972: CSV
  • 1990: XML → WBXML, ...
  • 2001: JSON BSON, BJSON, UBJSON, ...
  • 2001: YAML

 

JSON: Then and now

2001: State Software:

Douglas Crockford,
Chip Morningstar &
Robert F. Napiltonia

2013: ECMA-404 Standard

2014: RFC 7159 (Internet)

2002: json.org

2005-2006: 

JSON: Structure

Curly braces

Key

Value

Comma separated

Types:

Strings

Number

Arrays

null

JSON         XML

{
  "id": 123,
  "title": "Object Thinking",
  "author": "David West",
  "published": {
    "by": "Microsoft Press",
    "year": 2004
  }
}
<?xml version="1.0"?>
<book id="123">
  <title>Object Thinking</title>
  <author>David West</author>
  <published>
    <by>Microsoft Press</by>
    <year>2004</year>
  </published>
</book>

Documents

Data

SOAP

REST

AJAX

AJAJ

XML Schema + Transforms

JSON Schema

Querying JSON

From zero to hero

Querying JSON

1. Pure Javascript

(From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  { },
  { id: null },
  { id: NaN },
  { id: 'undefined' }
];

var invalidEntries = 0;

function filterByID(obj) {
  if ('id' in obj && typeof(obj.id) === 'number' && !isNaN(obj.id)) {
    return true;
  } else {
    invalidEntries++;
    return false;
  }
}

var arrByID = arr.filter(filterByID);

console.log('Filtered Array\n', arrByID); 
// Filtered Array
// [{ id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }]

console.log('Number of Invalid Entries = ', invalidEntries); 
// Number of Invalid Entries = 4

Querying JSON

2. Underscore

(From http://underscorejs.org)

var activeUsers = _.filter(users, function(user) { return user.isActive });
_.where(listOfPlays, {author: "Shakespeare", year: 1611});

// [{title: "Cymbeline", author: "Shakespeare", year: 1611},
//  {title: "The Tempest", author: "Shakespeare", year: 1611}]
_.findWhere(publicServicePulitzers, {newsroom: "The New York Times"});

/* {year: 1918, newsroom: "The New York Times",
  reason: "For its public service in publishing in full so many official reports,
  documents and speeches by European statesmen relating to the progress and
  conduct of the war."} */
var inactiveUsers = _.reject(users, function(user) { return user.isActive });

Querying JSON

3. json-query

(From https://github.com/mmckegg/json-query#jsonqueryquery-options)

var jsonQuery = require('json-query');

...

var data = {
  people: [
    {name: 'Matt', country: 'NZ'},
    {name: 'Pete', country: 'AU'},
    {name: 'Mikey', country: 'NZ'}
  ]
}

jsonQuery('people[country=NZ].name', {
  data: data
}) // {value: 'Matt', parents: [...], key: 0} ... etc

Querying JSON

4. DefiantJS

(From http://defiantjs.com/)

var obj = {
        "car": [
            {"@id": 10, "@color": "silver", "name": "Volvo"},
            {"@id": 11, "@color": "red",    "name": "Saab"},
            {"@id": 12, "@color": "red",    "name": "Peugeot"},
            {"@id": 13, "@color": "yellow", "name": "Porsche"}
        ],
        "bike": [
            {"@id": 20, "@color": "black", "name": "Cannondale"},
            {"@id": 21, "@color": "red",   "name": "Shimano"}
        ]
    },
    search = JSON.search(obj, '//car[@color="yellow"]/name');

console.log( search );
// ["Porsche"]

var reds = JSON.search(obj, '//*[@color="red"]');

for (var i=0; i<reds.length; i++) {
    console.log( reds[i].name );
}
// Saab
// Peugeot
// Shimano

XPath → JSONPath

Querying JSON

5. jQ

(From https://stedolan.github.io/jq/tutorial/)

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | 
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

https://jqplay.org/

Querying JSON

6. Others

json:select: https://github.com/lloyd/JSONSelect/blob/master/JSONSelect.md

JSON SQL: http://www.trentrichardson.com/jsonsql/

linq.js: https://linqjs.codeplex.com/

Q & A

gtrujillo@wearegap.com

www.growthaccelerationpartners.com

www.facebook.com/gapcr/

Thanks for your time!

Querying JSON

By gabby_tee

Querying JSON

  • 1,180