GithubHelp home page GithubHelp logo

json-glat's Introduction

json glat

json扁平化

npm i json-glat
const json_glat = require("json-glat");

json_glat.parse(json,options|string)

{
    "split": "/", // string default "."
    "root": "param", // string default undefined
    "noarr": false, // bool default false
    "handle": null, // function(key:string):string default undefined
}

ex1:

var a = json_glat.parse(
    {
      "a/b": 1,
      "/c": 2,
      "d": "aaa"
    },
    "/"
  );
console.log(JSON.stringify(a))

result:

{"a":{"b":1},"c":2,"d":"aaa"}

ex2:

var a = json_glat.parse(
  {
    "a/b": 1,
    "/c": 2,
    d: "aaa"
  },
  {
    split: "/",
    root: "result"
  }
);
console.log(JSON.stringify(a))

result:

{"result":{"a":{"b":1},"d":"aaa"},"c":2}

ex3:

var a = json_glat.parse(
  {
    "0/b": 1,
    "1/c": 2,
    "0/a": "aaa"
  },
  {
    split: "/",
    root: "result",
    // noarr : true
  }
);
console.log(JSON.stringify(a))

result:

{"result":[{"b":1,"a":"aaa"},{"c":2}]}
// {"result":{"0":{"b":1,"a":"aaa"},"1":{"c":2}}}

json_glat.glat(json,options|string)

{
    "split": "/", // string default "."
    "root": "param", // string default undefined
    "depth": 4, // number default -1
    "handle": null, // function(key:string[]):string[] default undefined
}

ex4:

var a = json_glat.glat(
  {
    a: {
      b: 1,
      c: "aaa",
      d: {
        h: "mm"
      }
    },
    c: 3
  },
  "_"
);
{"a_b":1,"a_c":"aaa","a_d_h":"mm","c":3}

ex5:

var a = json_glat.glat(
  {
    a: {
      b: 1,
      c: [1, 2, 3, 4],
      d: {
        h: "mm"
      }
    },
    c: 3
  },
  {
    split: "_",
    root: "a",
    // depth: 2
  }
);

console.log(JSON.stringify(a));
{"b":1,"c_0":1,"c_1":2,"c_2":3,"c_3":4,"d_h":"mm","_c":3}
// {"b":1,"c":[1,2,3,4],"d":{"h":"mm"},"_c":3}

ex5:

var a = json_glat.glat(
  {
    a: {
      b: 1,
      c: [1, 2, 3, 4],
      d: {
        h: "mm"
      }
    },
    c: 3
  },
  {
    split: "_"
  }
}

console.log(JSON.stringify(a));
{"a_b":1,"a_c_0":1,"a_c_1":2,"a_c_2":3,"a_c_3":4,"a_d_h":"mm","c":3}

json-glat's People

Contributors

xiaoyifan6 avatar arjunshibu avatar jamieslome avatar

Watchers

James Cloos avatar  avatar

Forkers

418sec

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.