At that point, you are just being unreasonable to keep marching on your crusade.
Frankly, this ain’t kind of crusade I want to join.
You should really rethink how you design your query. If I remember correctly you already have normalized tables and those JSON types are just coming from the view. This means you are creating problems yourself. You currently don’t have a heterogeneous schema, so why bother using JSON?
Anyway, this hasn’t been thoroughly tested but works for your case.
function unstringify(val) {
try {
const parsedValue = JSON.parse(val)
switch(true) {
case Array.isArray(parsedValue):
return parsedValue.map(unstringify)
case typeof parsedValue === 'object':
return Object.entries(parsedValue).reduce((o, [key, val]) => {
o[key] = unstringify(val)
return o
}, {})
default:
return parsedValue
}
} catch(e) {
return val;
}
}
function parse(obj) {
return Object.entries(obj).reduce((o, [key, val]) => {
o[key] = unstringify(val)
return o
}, {})
}
console.log('TEST unstringify')
const test_data = JSON.stringify({
str: 'hello',
num: 1,
bool: true,
arr: JSON.stringify([1, 2]),
arr_arr: JSON.stringify([
JSON.stringify([1, 2]),
JSON.stringify([1, 2])
]),
obj: JSON.stringify({ a: 1, b: 2 }),
obj_with_arr: JSON.stringify({
a: JSON.stringify([1, 2])
}),
arr_objs: JSON.stringify([
JSON.stringify({ a: 1, b: 2 }),
JSON.stringify({ a: 1, b: 2 })
]),
arr_obj_with_arr: JSON.stringify([
JSON.stringify({
a: JSON.stringify([1, 2])
})
]),
arr_obj_with_arr_obj: JSON.stringify([
JSON.stringify({
a: JSON.stringify([
JSON.stringify({ a: 1, b: 2 }),
JSON.stringify({ a: 1, b: 2 })
])
})
])
})
console.log(unstringify(test_data).arr_obj_with_arr_obj[0])
console.log('\nTest parse')
const your_data = [
{
"post_id":8,
"type":1,
"author":"Marta",
"categories":"[\"ludzie\"]",
"tags":"[\"accesories\",\"handcraft\"]",
"featured_img":"{\"file\": \"1.jpg\", \"2x\": null}",
"images":'["{\\"file\\": \\"Fiolet i zieleń.png\\", \\"2x\\": null, \\"3x\\": null, \\"pinIt\\": false, \\"alt\\": \\"Piękny obraz\\", \\"figcaption\\": null}","{\\"file\\": \\"Moja Japonia.jpg\\", \\"2x\\": null, \\"3x\\": null, \\"pinIt\\": true, \\"alt\\": \\"aaa\\", \\"figcaption\\": null}"]',
},
{
"post_id":8,
"type":1,
"author":"Marta",
"categories":"[\"ludzie\"]",
"tags":"[\"accesories\",\"handcraft\"]",
"featured_img":"{\"file\": \"1.jpg\", \"2x\": null}",
"images":'["{\\"file\\": \\"Fiolet i zieleń.png\\", \\"2x\\": null, \\"3x\\": null, \\"pinIt\\": false, \\"alt\\": \\"Piękny obraz\\", \\"figcaption\\": null}","{\\"file\\": \\"Moja Japonia.jpg\\", \\"2x\\": null, \\"3x\\": null, \\"pinIt\\": true, \\"alt\\": \\"aaa\\", \\"figcaption\\": null}"]',
}
]
const parsed = your_data.map(parse)
console.log(parsed)
console.log(parsed[0].images)