Home > Enterprise >  Get json data structure with nodejs
Get json data structure with nodejs

Time:02-28

This is a problem that is so hard for me to find using just keywords -- I've scrolled pages after pages and all the hits are on getting data from json structure, instead of getting data structure from json.

If you don't know what goal I'm trying to achieve, here are tools to get data structure from json to Go:

For one specific application, I'm getting data with all kinds of slightly different json data structures, which makes my data extraction failing all the times. I need to compare those data structure-wise, as each individual json data are surely different.

CodePudding user response:

It looks like you're struggling to find what you want because vanilla JavaScript doesn't have the type concepts you're trying to use.

You probably want to generate a json schema or a typescript interface, and searching "JSON to schema npm" will get you more useful results.

One such example is to-json-schema

import toJsonSchema from 'to-json-schema';

toJsonSchema({ "foo": "bar", "x": [1, 2] });
/* returns
{
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "x": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    }
  }
}
*/

For more specific answers you'd need to provide some minimal sample of input and output, e.g. what would you expect from { "foo": "bar", "x": [1, 2] }

  • Related