Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
convertir una forma de json en otra forma de objeto json usando javascript/node js

aquí data1 y el orden del content se cambian cada vez que utilicé la declaración de cambio. Estoy ejecutando una función al pasar un valor como este

 parser(values[key]);

este valor [clave]) contiene el siguiente objeto El siguiente objeto / estructura de datos

 { "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": {} } ] }

... debe convertirse en otra estructura personalizada como la siguiente ...

 [ { "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": {} } ] },

Ya se me ocurrió una declaración de cambio para verificar los datos entrantes y almacenarlos en el mismo formato o lugar que obtengo del primer objeto.

Mapeador.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; } } } };

y en jsonSchema estoy presionando los datos de retorno que obtuve de la función que escribí en un archivo diferente

Esquema.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 }], }; } }

el problema es que funciona perfectamente bien cuando el tipo es único, por ejemplo, si el tipo es barco o interno o subrayado, funciona bien, pero cuando el tipo contiene más de un tipo, entonces cómo se usa para hacer el resultado esperado que no es posible para mí.

ya que estoy buscando la solución si algún objeto contiene tipo: "negrita" tipo: cursiva, debe reemplazarse con negrita: cursiva verdadera: verdadero o subrayado o tipo interior: código

solo quería reemplazar este tipo y obtener el objeto json como se esperaba, pero no sé cómo hacerlo

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!