Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
converting one form of json into another form of json object using javascript / node js

here data1 and order of content get changed everytime that why I used switch statement I am running a function by passing value like this

parser(values[key]);

this value[key]) contain bellow object The following object / data-structure

  {
    "nodeType": "document",
    "data": {},
    "content": [
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "dummy testing bold",
            "marks": [
              {
                "type": "bold"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "jsdbhcjbjsdz'c;s bold and italic",
            "marks": [
              {
                "type": "bold"
              },
              {
                "type": "italic"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "underline bold and italic",
            "marks": [
              {
                "type": "bold"
              },
              {
                "type": "italic"
              },
              {
                "type": "underline"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "ldwc;s,l;,aws italic",
            "marks": [
              {
                "type": "italic"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "hr",
        "content": [],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "testing",
            "marks": [
              {
                "type": "italic"
              },
              {
                "type": "code"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "unordered-list",
        "content": [
          {
            "nodeType": "list-item",
            "content": [
              {
                "nodeType": "paragraph",
                "content": [
                  {
                    "nodeType": "text",
                    "value": "hello ji",
                    "marks": [
                      {
                        "type": "italic"
                      },
                      {
                        "type": "code"
                      }
                    ],
                    "data": {}
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          },
          {
            "nodeType": "list-item",
            "content": [
              {
                "nodeType": "paragraph",
                "content": [
                  {
                    "nodeType": "text",
                    "value": "nahi ji",
                    "marks": [
                      {
                        "type": "italic"
                      },
                      {
                        "type": "code"
                      }
                    ],
                    "data": {}
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          },
          {
            "nodeType": "list-item",
            "content": [
              {
                "nodeType": "paragraph",
                "content": [
                  {
                    "nodeType": "text",
                    "value": "testing ji",
                    "marks": [
                      {
                        "type": "underline"
                      },
                      {
                        "type": "italic"
                      },
                      {
                        "type": "bold"
                      }
                    ],
                    "data": {}
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "ordered-list",
        "content": [
          {
            "nodeType": "list-item",
            "content": [
              {
                "nodeType": "paragraph",
                "content": [
                  {
                    "nodeType": "text",
                    "value": "testing",
                    "marks": [
                      {
                        "type": "italic"
                      },
                      {
                        "type": "code"
                      }
                    ],
                    "data": {}
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          },
          {
            "nodeType": "list-item",
            "content": [
              {
                "nodeType": "paragraph",
                "content": [
                  {
                    "nodeType": "text",
                    "value": "newh",
                    "marks": [
                      {
                        "type": "italic"
                      },
                      {
                        "type": "code"
                      }
                    ],
                    "data": {}
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "blockquote",
        "content": [
          {
            "nodeType": "paragraph",
            "content": [
              {
                "nodeType": "text",
                "value": "blockquotes",
                "marks": [
                  {
                    "type": "italic"
                  },
                  {
                    "type": "code"
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          },
          {
            "nodeType": "paragraph",
            "content": [
              {
                "nodeType": "text",
                "value": "blockquotes",
                "marks": [
                  {
                    "type": "bold"
                  },
                  {
                    "type": "code"
                  },
                  {
                    "type": "underline"
                  }
                ],
                "data": {}
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-1",
        "content": [
          {
            "nodeType": "text",
            "value": "Heading 1",
            "marks": [
              {
                "type": "bold"
              },
              {
                "type": "underline"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-2",
        "content": [
          {
            "nodeType": "text",
            "value": "heading 2",
            "marks": [
              {
                "type": "italic"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-3",
        "content": [
          {
            "nodeType": "text",
            "value": "heading 3",
            "marks": [
              {
                "type": "italic"
              },
              {
                "type": "underline"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-4",
        "content": [
          {
            "nodeType": "text",
            "value": "heading 4",
            "marks": [
              {
                "type": "bold"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-5",
        "content": [
          {
            "nodeType": "text",
            "value": "heading 5",
            "marks": [
              {
                "type": "underline"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "heading-6",
        "content": [
          {
            "nodeType": "text",
            "value": "heading 6",
            "marks": [
              {
                "type": "italic"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "hr",
        "content": [],
        "data": {}
      },
      {
        "nodeType": "embedded-entry-block",
        "content": [],
        "data": {
          "target": {
            "sys": {
              "id": "651dYO8d4oE0MVJlVePWr6",
              "type": "Link",
              "linkType": "Entry"
            }
          }
        }
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          },
          {
            "nodeType": "hyperlink",
            "content": [
              {
                "nodeType": "text",
                "value": "Google",
                "marks": [],
                "data": {}
              }
            ],
            "data": {
              "uri": "www.google.com"
            }
          },
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          },
          {
            "nodeType": "entry-hyperlink",
            "content": [
              {
                "nodeType": "text",
                "value": "Entry",
                "marks": [],
                "data": {}
              }
            ],
            "data": {
              "target": {
                "sys": {
                  "id": "66rzYr2BpWL1VTBHdLTdSW",
                  "type": "Link",
                  "linkType": "Entry"
                }
              }
            }
          },
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          },
          {
            "nodeType": "asset-hyperlink",
            "content": [
              {
                "nodeType": "text",
                "value": "Asset",
                "marks": [],
                "data": {}
              }
            ],
            "data": {
              "target": {
                "sys": {
                  "id": "2FfbMwKRZ2QK52MsbYxZbb",
                  "type": "Link",
                  "linkType": "Asset"
                }
              }
            }
          },
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "hi code here",
            "marks": [
              {
                "type": "code"
              }
            ],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "embedded-entry-block",
        "content": [],
        "data": {
          "target": {
            "sys": {
              "id": "4zu7f2YP4cXqR6aeeMxEt8",
              "type": "Link",
              "linkType": "Entry"
            }
          }
        }
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          },
          {
            "nodeType": "embedded-entry-inline",
            "content": [],
            "data": {
              "target": {
                "sys": {
                  "id": "4zu7f2YP4cXqR6aeeMxEt8",
                  "type": "Link",
                  "linkType": "Entry"
                }
              }
            }
          },
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      },
      {
        "nodeType": "embedded-entry-block",
        "content": [],
        "data": {
          "target": {
            "sys": {
              "id": "2eAmIIuG4xkLvatkU3RUSy",
              "type": "Link",
              "linkType": "Entry"
            }
          }
        }
      },
      {
        "nodeType": "paragraph",
        "content": [
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          },
          {
            "nodeType": "embedded-entry-inline",
            "content": [],
            "data": {
              "target": {
                "sys": {
                  "id": "49VJ3thpTULacWCcnjbOXz",
                  "type": "Link",
                  "linkType": "Entry"
                }
              }
            }
          },
          {
            "nodeType": "text",
            "value": "",
            "marks": [],
            "data": {}
          }
        ],
        "data": {}
      }
    ]
  }

... should be converted into another custom structure like the following one ...

 [
    {
      "type": "doc",
      "attrs": { "dirty": true },
      "children": [
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "dummy testing bold", "bold": true }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [
            {
              "text": "jsdbhcjbjsdz'c;s bold and italic",
              "bold": true,
              "italic": true
            }
          ]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [
            {
              "text": "underline bold and italic",
              "bold": true,
              "italic": true,
              "underline": true
            }
          ]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "idwc:s,l:.aws italic", "italic": true }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "italic": true, "text": "" }]
        },
        {
          "type": "hr",
          "children": [{ "text": "" }],
          "attrs": { "dirty": true }
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "" }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "testing", "italic": true }]
        },
        {
          "type": "ul",
          "children": [
            {
              "type": "li",
              "attrs": { "dirty": true },
              "children": [{ "italic": true, "text": "hello ji" }]
            },
            {
              "type": "li",
              "attrs": { "dirty": true },
              "children": [{ "italic": true, "text": "nahi ji" }]
            },
            {
              "type": "li",
              "attrs": { "dirty": true },
              "children": [
                {
                  "text": "testing ji",
                  "italic": true,
                  "bold": true,
                  "underline": true
                }
              ]
            }
          ],
          "id": "6c180306f6204829999b78e8dd482d33",
          "attrs": { "dirty": true }
        },
        {
          "type": "ol",
          "children": [
            {
              "type": "li",
              "children": [{ "text": "testing", "italic": true }],
              "attrs": { "dirty": true }
            },
            {
              "type": "li",
              "attrs": { "dirty": true },
              "children": [{ "italic": true, "text": "newh" }]
            }
          ],
          "id": "10207a886ca241419b66a3b155bac028",
          "attrs": { "dirty": true }
        },
        {
          "type": "blockquote",
          "children": [
            { "text": "blockquotes\n\n", "italic": true, "underline": true },
            { "text": "blockquotes", "underline": true, "bold": true }
          ],
          "attrs": { "dirty": true }
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "" }]
        },
        {
          "type": "h1",
          "attrs": { "dirty": true },
          "children": [{ "text": "Heading 1", "bold": true, "underline": true }]
        },
        {
          "type": "h2",
          "attrs": { "dirty": true },
          "children": [{ "text": "heading 2", "italic": true }]
        },
        {
          "type": "h3",
          "attrs": { "dirty": true },
          "children": [
            { "text": "heading 3", "italic": true, "underline": true }
          ]
        },
        {
          "type": "h4",
          "attrs": { "dirty": true },
          "children": [{ "text": "heading 4", "bold": true }]
        },
        {
          "type": "h5",
          "attrs": { "dirty": true },
          "children": [{ "text": "heading 5", "underline": true }]
        },
        {
          "type": "h6",
          "attrs": { "dirty": true },
          "children": [{ "text": "heading 6", "italic": true }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "italic": true, "text": "" }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "italic": true, "text": "" }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [
            { "italic": true, "text": "" },
            {
              "type": "a",
              "attrs": {
                "url": "www.google.com",
                "target": "_self",
                "dirty": true
              },
              "children": [{ "text": "Google" }]
            },
            { "text": "" }
          ]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [{ "text": "" }]
        },        
        {
          "type": "code",
          "attrs": { "language": "html", "dirty": true },
          "children": [{ "text": "hi code here" }]
        },
        {
          "type": "p",
          "attrs": { "dirty": true },
          "children": [
            { "text": "" },
            {
              "type": "reference",
              "attrs": {
                "display-type": "inline",
                "type": "entry",
                "class-name": "embedded-entry redactor-component inline-entry",
                "entry-uid": "bltccc6338d24478e6f",
                "locale": "en-us",
                "content-type-uid": "rte",
                "dirty": true
              },
              "children": [{ "text": "" }]
            },
            { "text": "" }
          ]
        }
      ]
    }
  ]

  {
            "nodeType": "document",
            "data": {},
            "content": [
                {
                    "nodeType": "ordered-list",
                    "content": [
                        {
                            "nodeType": "list-item",
                            "content": [
                                {
                                    "nodeType": "paragraph",
                                    "content": [
                                        {
                                            "nodeType": "text",
                                            "value": "order list value 1",
                                            "marks": [],
                                            "data": {}
                                        }
                                    ],
                                    "data": {}
                                }
                            ],
                            "data": {}
                        },
                        {
                            "nodeType": "list-item",
                            "content": [
                                {
                                    "nodeType": "paragraph",
                                    "content": [
                                        {
                                            "nodeType": "text",
                                            "value": "order list value 2 with bold and italic",
                                            "marks": [
                                                {
                                                    "type": "bold"
                                                },
                                                {
                                                    "type": "italic"
                                                }
                                            ],
                                            "data": {}
                                        }
                                    ],
                                    "data": {}
                                }
                            ],
                            "data": {}
                        }
                    ],
                    "data": {}
                },
                {
                    "nodeType": "heading-3",
                    "content": [
                        {
                            "nodeType": "text",
                            "value": "with Heading 3 value",
                            "marks": [],
                            "data": {}
                        }
                    ],
                    "data": {}
                },
                {
                    "nodeType": "paragraph",
                    "content": [
                        {
                            "nodeType": "text",
                            "value": "hi normal text",
                            "marks": [],
                            "data": {}
                        }
                    ],
                    "data": {}
                },
                {
                    "nodeType": "paragraph",
                    "content": [
                        {
                            "nodeType": "text",
                            "value": "bold text here",
                            "marks": [
                                {
                                    "type": "bold"
                                }
                            ],
                            "data": {}
                        }
                    ],
                    "data": {}
                },
                {
                    "nodeType": "paragraph",
                    "content": [
                        {
                            "nodeType": "text",
                            "value": "bold and italic same data test",
                            "marks": [
                                {
                                    "type": "bold"
                                },
                                {
                                    "type": "italic"
                                }
                            ],
                            "data": {}
                        }
                    ],
                    "data": {}
                }
            ]
        },

I already came up with a switch statement in order to check the incoming data and store it in the same format or place which I am getting from the first object

Mapper.js

const {
  bold,
  italic,
  underline,
  h1,
  h2,
} = require("./Schema");

module.exports = jsonRTEMapper = (data) => {
  const jsonSchema = [];
  for (const nodeType of data) {
    if (
      nodeType.content.length !== undefined &&
      nodeType.content.length !== 0
    ) {
      switch (nodeType.nodeType) {
        case "paragraph":
          for (const mark of nodeType.content) {
            if (
              mark.marks !== undefined &&
              mark.marks.length !== undefined &&
              mark.marks.length !== 0
            ) {
              for (const type of mark.marks) {
                switch (type.type) {
                  case "bold":
                    jsonSchema.push(bold(nodeType.content));
                    break;
                  case "italic":
                    jsonSchema.push(italic(nodeType.content));
                    break;
                  case "underline":
                    jsonSchema.push(underline(nodeType.content));
                    break;
                  case "code":
                    jsonSchema.push(code(nodeType.content));
                    break;
                }
              }
            }
          }
          break;
        case "heading-1":
          jsonSchema.push(h1(nodeType.content));
          break;
        case "heading-2":
          jsonSchema.push(h2(nodeType.content));
          break;     
      }
    }
  }
};

and in jsonSchema I am pushing the return data which I got from the function which I wrote in different file

Schema.js

function bold(data) {
  for (const value of data) {
    return {
      type: "p",
      attrs: { dirty: true },
      children: [{ text: value.value, bold: true }],
    };
  }
}
function italic(data) {
  for (const value of data) {
    return {
      type: "p",
      attrs: { dirty: true },
      children: [{ text: value.value, italic: true }],
    };
  }
}
function h1(data) {
  for (const value of data) {
    return {
      type: "h1",
      attrs: { dirty: true },
      children: [{ text: value.value }],
    };
  }
}
function h2(data) {
  for (const value of data) {
    return {
      type: "h2",
      attrs: { dirty: true },
      children: [{ text: value.value }],
    };
  }
}

the problem is it is working perfectly fine when the type is single for eg if type is boat or internal or underline it work fine but when the type contain more then one type then how to use make the expected output that is not possible by me

as I am looking of the solution if any object contain type:"bold" type:italic it should replace with the bold:true italic:true or underline or inside type:code

just wanted to replace this type and fetch the json object as expected json object but don't know how to do it

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda