Home > OS >  How to edit JSON Arrays?
How to edit JSON Arrays?

Time:06-01

I want to change all elements in the array, deleting keys I do not need and also turning the names into keys for simplicity. How do you go about that? Is there a simple piece of software or a website where you can edit JSON arrays in that manner?

What I mean is that I want to turn something like this:

"一": {
    "strokes": 1,
    "grade": 1,
    "freq": 2
},
"二": {
    "strokes": 2,
    "grade": 1,
    "freq": 9
}

Into something like this:

{
    "symbol":"一",
    "strokes": 1,
    "grade": 1
},
{
    "symbol": 二,
    "strokes": 2,
    "grade": 1
}

So yeah, what tool do you use to do something like that?

CodePudding user response:

If you're asking to recommend a tool, then the question is off-topic for Stack Overflow.

It can easily be done in one JavaScript statement though, so here's your tool:

const json = {
  "一": {
      "strokes": 1,
      "grade": 1,
      "freq": 2
  },
  "二": {
      "strokes": 2,
      "grade": 1,
      "freq": 9
  }
};

const result = Object.entries(json).map(([symbol, { strokes, grade }]) =>
  ({ symbol, strokes, grade }));

console.log(result);

  • Related