简体   繁体   中英

How to parse a special JSON format to JavaScript object?

I have been searching for an answer to this specific kind of format, but was not able to find any to solve this specific issue.

The situation is, I have a kind of JSON format that I can't work with in combination with mongoDB. I wish to alter the format of the JSON data to a normal JavaScript object. Now the data is over 2,000 entries long, so I can't handle it manually. And I couldn't make the JSON.parse(data) work for this kind of special format.

Here an example of the current JSON format:

{
    "一": {
        "strokes": 1,
        "grade": 1,
        "freq": 2,
        "jlpt_old": 4,
        "jlpt_new": 5,
        "meanings": ["One","One Radical (no.1)"],
        "readings_on": ["いち","いつ"],
        "readings_kun": ["ひと-","ひと.つ"],
        "wk_level": 1,
        "wk_meanings": ["One"],
        "wk_readings_on": ["いち","いつ"],
        "wk_readings_kun": ["!ひと"],
        "wk_radicals": ["Ground"]
    },
    "二": {
        "strokes": 2,
        "grade": 1,
        "freq": 9,
        "jlpt_old": 4,
        "jlpt_new": 5,
        "meanings": ["Two","Two Radical (no. 7)"],
        "readings_on": ["に","じ"],
        "readings_kun": ["ふた","ふた.つ","ふたたび"],
        "wk_level": 1,
        "wk_meanings": ["Two"],
        "wk_readings_on": ["に"],
        "wk_readings_kun": ["!ふた"],
        "wk_radicals": ["Two"]
    },
}

And the format I eventually wish to achieve is the following:

[
    {
        kanji: "一",
        strokes: 1,
        grade: 1,
        freq: 2,
        jlpt_old: 4,
        jlpt_new: 5,
        meanings: ["One","One Radical (no.1)"],
        readings_on: ["いち","いつ"],
        readings_kun: ["ひと-","ひと.つ"],
        wk_level: 1,
        wk_meanings: ["One"],
        wk_readings_on: ["いち","いつ"],
        wk_readings_kun: ["!ひと"],
        wk_radicals: ["Ground"]
    },
    {
        kanji: "二",
        strokes: 2,
        grade: 1,
        freq: 9,
        jlpt_old: 4,
        jlpt_new: 5,
        meanings: ["Two","Two Radical (no. 7)"],
        readings_on: ["に","じ"],
        readings_kun: ["ふた","ふた.つ","ふたたび"],
        wk_level: 1,
        wk_meanings: ["Two"],
        wk_readings_on: ["に"],
        wk_readings_kun: ["!ふた"],
        wk_radicals: ["Two"]
    }
]

As you can see, the initial format has a key describing each object, but the aspired format has every info inside the object.

It would be awesome if someone could help me out on this problem! :)

Both are actually valid JSON objects, although you are looking for a simple manipulation:

 const data = { "一": { "strokes": 1, "grade": 1, "freq": 2, "jlpt_old": 4, "jlpt_new": 5, "meanings": ["One","One Radical (no.1)"], "readings_on": ["いち","いつ"], "readings_kun": ["ひと-","ひと.つ"], "wk_level": 1, "wk_meanings": ["One"], "wk_readings_on": ["いち","いつ"], "wk_readings_kun": ["!ひと"], "wk_radicals": ["Ground"] }, "二": { "strokes": 2, "grade": 1, "freq": 9, "jlpt_old": 4, "jlpt_new": 5, "meanings": ["Two","Two Radical (no. 7)"], "readings_on": ["に","じ"], "readings_kun": ["ふた","ふた.つ","ふたたび"], "wk_level": 1, "wk_meanings": ["Two"], "wk_readings_on": ["に"], "wk_readings_kun": ["!ふた"], "wk_radicals": ["Two"] } }; const parsed = Object.entries(data).reduce((acc, [kanji, obj]) => acc.concat({kanji, ...obj}), []); console.log(parsed)

Is this good for you?

 let json = { "一": { "strokes": 1, "grade": 1, "freq": 2, "jlpt_old": 4, "jlpt_new": 5, "meanings": ["One","One Radical (no.1)"], "readings_on": ["いち","いつ"], "readings_kun": ["ひと-","ひと.つ"], "wk_level": 1, "wk_meanings": ["One"], "wk_readings_on": ["いち","いつ"], "wk_readings_kun": ["!ひと"], "wk_radicals": ["Ground"] }, "二": { "strokes": 2, "grade": 1, "freq": 9, "jlpt_old": 4, "jlpt_new": 5, "meanings": ["Two","Two Radical (no. 7)"], "readings_on": ["に","じ"], "readings_kun": ["ふた","ふた.つ","ふたたび"], "wk_level": 1, "wk_meanings": ["Two"], "wk_readings_on": ["に"], "wk_readings_kun": ["!ふた"], "wk_radicals": ["Two"] }, } let response = []; for(let key in json){ response.push({ kanji: key, ...json[key]}); } console.log(response);

A single map() call on the Object.entries() of the parsed JSON is all you need.

 const json = '{"一":{"strokes":1,"grade":1,"freq":2,"jlpt_old":4,"jlpt_new":5,"meanings":["One","One Radical (no.1)"],"readings_on":["いち","いつ"],"readings_kun":["ひと-","ひと.つ"],"wk_level":1,"wk_meanings":["One"],"wk_readings_on":["いち","いつ"],"wk_readings_kun":["!ひと"],"wk_radicals":["Ground"]},"二":{"strokes":2,"grade":1,"freq":9,"jlpt_old":4,"jlpt_new":5,"meanings":["Two","Two Radical (no. 7)"],"readings_on":["に","じ"],"readings_kun":["ふた","ふた.つ","ふたたび"],"wk_level":1,"wk_meanings":["Two"],"wk_readings_on":["に"],"wk_readings_kun":["!ふた"],"wk_radicals":["Two"]}}'; const obj = JSON.parse(json); const refactored = Object.entries(obj).map(([k, v]) => ({kanji: k, ...v})); console.log(refactored);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM