detect-json-style

    1.0.2 • Public • Published

    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'))
    })
     
     

    Keywords

    Install

    npm i detect-json-style

    DownloadsWeekly Downloads

    201

    Version

    1.0.2

    License

    MIT

    Last publish

    Collaborators

    • finnpauls
    • karissa