npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-to-avro

v1.1.1

Published

Helper functions to convert JSON objects to Avro according to your schema. And back to JSON.

Downloads

701

Readme

json-to-avro

TODO: Implement configure the key that is used to set and detect custom union multi record typed types.

Json to avro

To convert plain JSON to avro-serializable JSON, supply the avro schema in JSON format e.g:

const { jsonToAvro } = require('json-to-avro')

const AvroSchema = {
  type: 'record',
  name: 'test',
  fields: [
    {
      name: 'id',
      type: 'string',
      doc: 'test doc id',
    },
    {
      name: 'value',
      type: ['string', 'null'],
      default: '',
      doc: 'random value',
    },
    {
      doc: 'list of values',
      name: 'valuess',
      type: {
        type: 'array',
        items: 'string',
      },
    },
  ],
}

const JSONRecord = {
  id: 'abc1q24',
  value: 'value2',
  valuess: ['value4', 'value3'],
}

const avroRecord = jsonToAvro(AvroSchema, JSONRecord)

// avroRecord = {
//   valuess: ['value4', 'value3'],
//   value: { string: 'value2' },
//   id: 'abc1q24',
// }

Avro to json

And vice versa.

const { avroToJson } = require('json-to-avro')

const AvroSchema = {
  type: 'record',
  name: 'test',
  fields: [
    {
      name: 'id',
      type: 'string',
      doc: 'test doc id',
    },
    {
      name: 'value',
      type: ['string', 'null'],
      default: '',
      doc: 'random value',
    },
    {
      doc: 'list of values',
      name: 'valuess',
      type: {
        type: 'array',
        items: 'string',
      },
    },
  ],
}

const AvroRecord = {
  valuess: ['value4', 'value3'],
  value: { string: 'value2' },
  id: 'abc1q24',
}

const JSONRecord = avroToJson(AvroSchema, AvroRecord)

// JSONRecord = {
//   id: 'abc1q24',
//   value: 'value2',
//   valuess: ['value4', 'value3'],
// }

Note

If you create a schema that holds a structure like in the example down below, with an array of items that holds multiple different record types as options, the items in the converted json will receive an extra field __type with the name of the record type as value. If we don't add this field you will not be able to differentiate between the record types in the actions array.

{
  "name": "actions",
  "doc": "List of actions to be executed for this annotation",
  "type": {
    "type": "array",
    "items": [
      {
        "name": "UiScoreboardVisibilityAction",
        "type": "record",
        "doc": "action for changing Scoreboard visibility",
        "fields": [
          {
            "name": "visible",
            "doc": "Defines the visibility status for scoreboard",
            "type": "boolean"
          }
        ]
      },
      {
        "name": "UiTimerVisibilityAction",
        "type": "record",
        "doc": "action for changing Timer visibility",
        "fields": [
          {
            "name": "visible",
            "doc": "Defines the visibility status for timer",
            "type": "boolean"
          }
        ]
      }
    ]
  }
}

// Avro serializable json
{
  "actions": [
    {
      "UiTimerVisibilityAction": {
        "visible": true
      }
    },
    {
      "UiScoreboardVisibilityAction": {
        "visible": true
      }
    }
  ]  
}
// Plain parsed json
{
  "actions": [
    {
      "visible": true
    },
    {
      "visible": true
    }
  ]
}

// Parsed by avro to json.
{
  "actions": [
    {
      "__type": "UiTimerVisibilityAction",
      "visible": true
    },
    {
      "__type": "UiScoreboardVisibilityAction",
      "visible": true
    }
  ]
}

Vice versa, if you want to convert plain JSON to avro, you must add the __type field to be in sync the record type name.