Hi All,
I was looking to use GraphQL to update a rich text metafield, however found that the metafield required JSON but not in a format converted directly from HTML.
I looked for a solution but failed to find one, I implemented my own solution in PHP and thought I’d share here in case anyone else needs it or could improve my code.
An example of the JSON required by Shopify:
{
"type": "root",
"children": [
{
"type": "heading",
"children": [
{
"type": "text",
"value": "Heading"
}
],
"level": 1
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "bold ",
"bold": true
},
{
"type": "text",
"value": "normal text "
},
{
"type": "text",
"value": "italics",
"italic": true
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": ""
},
{
"url": "link url",
"title": "link title",
"type": "link",
"children": [
{
"type": "text",
"value": "link",
"italic": true
}
]
},
{
"type": "text",
"value": ""
}
]
},
{
"listType": "unordered",
"type": "list",
"children": [
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "unordered list 1"
}
]
},
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "unordered list 2"
}
]
}
]
},
{
"listType": "ordered",
"type": "list",
"children": [
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "list item 1"
}
]
},
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "list item 2"
}
]
}
]
}
]
}