detect-json-style

Detect style of formatted tabular data from a first chunk

Usage no npm install needed!

<script type="module">
  import detectJsonStyle from 'https://cdn.skypack.dev/detect-json-style';
</script>

README

detect-json-style

Windows Mac/Linux
Windows Build status Build Status

Detect JSON type from a first peek chunk of a string.

It detects the following styles

  • object Array top-level nested in an object like {"rows": [{"a": 1}, ..]
  • array Array of objects [{"a": 1}...]
  • multiline JSON objects after each other {"a: 1"}{"a": 2} (can be ndjson)

usage

var detectJSON = require('detect-json-style')
var json = detectJSON('{"rows": [{"a": 1}, {"a": 2}, {"a":')
// json -> {style: 'object', selector: 'rows.*'}

Works well together with peek-stream and JSONStream. The selector attribute is compatible with the first argument of JSONStream.parse().

var peek = require('peek-stream')
var JSONStream = require('JSONStream')
var detectJSON = require('detect-json-style')

peek(function (data, swap) {
  var json = detectJSON(swap)
  if(json) swap(null, JSONStream.parse(json.selector))
    
  swap(new Error('Could not determine format'))
})