0

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! :)

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks MorKadosh! That really helped out a lot! :)
0

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);

2 Comments

Thank you pilchard for this compact solution! Very nice to know indeed! :)
No worries. Since you are returning an array of the same length as the source array it is better to use map() than reduce().
0

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);

1 Comment

Thank you Ran Marciano, the solution is very easy to understand. Was a great help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.