Re-organizing the JSON output to a "Simple" format

Describe the problem/error/question

I have the following JSON that has different values has a label, I am trying to “normalize” in order to create an Array List, followed a couple of topics here, but what I applied so far is not working.

[
    {
        "category": {
            "name": "Categories",
            "types": [
                "post"
            ]
        },
        "post_tag": {
            "name": "Tags",
            "types": [
                "post",
                "event",
                "place"
            ]
        },
        "nav_menu": {
            "name": "Navigation Menus",
            "types": [
                "nav_menu_item"
            ]
        },
        "wp_pattern_category": {
            "name": "Pattern Categories",
            "types": [
                "wp_block"
            ]
        }
    }
]

What is the error message (if any)?

I am trying to join the data to a list format such where the highest hierarchy values will be set “linear” as “Type” as below:

[
    {
        "Type": "Category",
        "name": "Categories",
        "types": [
            "post"
        ]
    },
    {
        "Type": "post_tag",
        "name": "Tags",
        "types": [
            "post",
            "event",
            "place"
        ]
    },
    {
        "Type": "nav_menu",
        "name": "Navigation Menus",
        "types": [
            "nav_menu_item"
        ]
    },
    {
        "Type": "wp_pattern_category",
        "name": "Pattern Categories",
        "types": [
            "wp_block"
        ]
    }
]

Thank you very much in advance for the help.

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup - 1.16.0

  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
1 Like

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Jorge_M
this line will do the trick:

const newObjects = Object.entries(item.json).map(([key, value]) => ({
  Type: key,
  ...value
}));

It parses all keys of a json-object to a list of objects.

This is the full workflow:

For this kind of questions ChatGPT is really handy. Even the free GPT3.5 model can handle that kind of strutured questions really well.

Cheers.

3 Likes

Thanks @nico-kow, I did try before chatGPT(Well, BING AI), didn’t worked, but maybe was not giving a proper “term” explanation :slight_smile: .

Thanks a lot, this worked perfectly :partying_face:.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.