'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
Solution 1:[1]
If you put all the mappers/parsers in a map you won't have to use switch case statements. And things will be easier to implement:
// parsers.js ------------------------------
// put all parsing logic in this file
const parsers = new Map();
// once all the parsers are implemented you don't need this check
parsers._get = parsers.get;
parsers.get = function(key) {
if (this.has(key))
return this._get(key);
return () => console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
}
// map all the parsers
// keys should be exactly same as keys in input json
parsers.set('document', parseDocument);
parsers.set('paragraph', parseParagraph);
parsers.set('text', parseText);
parsers.set('hr', parseHR);
parsers.set('list-item', parseLI);
parsers.set('unordered-list', parseUL);
parsers.set('ordered-list', parseOL);
// TODO: implement remaining 15 element types
//parsers.set('', parse);
function convert(obj) {
return [parsers.get(obj.nodeType)(obj)];
};
function parseDocument(obj) {
let type = 'doc';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseParagraph(obj) {
let type = 'p';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseText(obj) {
const result = {};
result.text = obj.value;
obj.marks.forEach(e => result[e.type] = true);
return result;
}
function parseHR(obj) {
let type = 'hr';
let attrs = { "dirty": true }
let children = [];
return { type, attrs, children };
}
function parseUL(obj) {
let type = 'ul';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseOL(obj) {
let type = 'ol';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseLI(obj) {
let type = 'li';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
// get rid of paragraph elements
children = children.map(c => c.children).flat();
return { type, children, attrs };
}
// mapper.js ---------------------------------------------
let result = convert(getSrcData());
console.log('Converted object: ', result);
let result2 = convert(getSrcData2());
console.log('Converted object2: ', result2);
function getSrcData() {
return {
"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": {}
}
]
}
}
function getSrcData2() {
return {
"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": {}
}
]
};
}
<b>See console logs for the output.</b>
Explanation is in code comments. I've laid down the framework and implemented parsers for 6 element types. Rest you can implement easily. While implementing remaining types you'll understand the code very well.
- Use Developer tool's code debugging features to understand the code. Or use VSCode to debug nodejs code.
- Use http://jsonviewer.stack.hu to analyze input and output JSONs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |