简体   繁体   中英

Extract images from JSON string

Task: Treat this as a string and copy all images present in the string to any database of your choosing and rebuild the string with the new URL

{"_items": [{"_id": "5f5b6acab4175c979b8cb5af", "owning_partner_id": 10304, "type": "Question", "subtype": "Subjective", "modifier": "Judgemental", "status": "Draft", "author_id": 4542, "assigned_to": 4542, "content": {"question_details": {"en": {"answers": [{"body": "", "is_correct": true, "explanation": ".", "code": "a"}], "question_txt": "

In the figure, a rectangular block is gradually pushed face down into a liquid. The block has height <math xmlns="http://www.w3.org/1998/Math/MathML">d; on the bottom and top the face area is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">A=8.00 cm2. In the figure, gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ?

\n\n
<img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.lusIzGK6zcFw1rxBK38KHQHaE8%26pid%3DApi&f=1" style="width: 245px; height: 150px;" />  <img src="https://static.theceomagazine.net/wp-content/uploads/2018/10/15093202/elon-musk-1100x733.jpg" style="width: 245px; height: 197px;" /> 

\n"}}, "book_toc": [], "question_meta_tags": [{"difficulty_level": "", "primary_concept": "", "ideal_time": "", "secondary_concept": [], "learning_maps": [], "learning_maps_data": []}]}, "superseded_by": null, "language": "en", "is_atg_ready": 0, "created_by": 4542, "updated_by": 4542, "is_prof_approved": 0, "content_schema_version": 1, "version": 1, "_updated": "Fri, 11 Sep 2020 12:17:14 GMT", "_created": "Fri, 11 Sep 2020 12:17:14 GMT", "_version": 1, "tenant_id": 1, "title": "
In the figure, a rectangular block is gradually pushed face down into a liquid. The block has height <math xmlns="http://www.w3.org/1998/Math/MathML">d; on the bottom and top the face area is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">A=8.00 cm2. In the figure, gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ?

\n\n
<img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 150px;" />  <img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 197px;" /> 

\n", "id": 6126381, "question_code": "EM6126381-en", "uuid": "d00dfd63-082e-4062-8f1f-e37e7c9f0829", "created_at": "Fri, 11 Sep 2020 12:17:14 GMT", "updated_at": "Fri, 11 Sep 2020 12:17:14 GMT", "_etag": "3027df270a05e67d65b254d9b134bab952cbcd68", "_links": {"self": {"title": "learning_objects", "href": "learning_objects/5f5b6acab4175c979b8cb5af"}}, "_latest_version": 1, "user_name": "reshma awale"}], "_links": {"parent": {"title": "home", "href": "/"}, "self": {"title": "learning_objects", "href": "learning_objects?where={"type":"Question","$or":[{"id":{"$in":[6126381]}},{"question_code":{"$in":[]}}]}&sort=id&embed=true"}}, "_meta": {"page": 1, "max_results": 25, "total": 1}}

Perhaps you meant this?

PS: Your JSON (if that is what it is supposed to be) is not valid dues to the \\n and the double quotes in the nested MathML

 let string = `{"_items": [{"_id": "5f5b6acab4175c979b8cb5af", "owning_partner_id": 10304, "type": "Question", "subtype": "Subjective", "modifier": "Judgemental", "status": "Draft", "author_id": 4542, "assigned_to": 4542, "content": {"question_details": {"en": {"answers": [{"body": "", "is_correct": true, "explanation": ".", "code": "a"}], "question_txt": " In the figure, a rectangular block is gradually pushed face down into a liquid. The block has height <math xmlns="http://www.w3.org/1998/Math/MathML">d; on the bottom and top the face area is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">A=8.00 cm2. In the figure, gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ? \\n\\n <img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.lusIzGK6zcFw1rxBK38KHQHaE8%26pid%3DApi&f=1" style="width: 245px; height: 150px;" /> <img src="https://static.theceomagazine.net/wp-content/uploads/2018/10/15093202/elon-musk-1100x733.jpg" style="width: 245px; height: 197px;" /> \\n"}}, "book_toc": [], "question_meta_tags": [{"difficulty_level": "", "primary_concept": "", "ideal_time": "", "secondary_concept": [], "learning_maps": [], "learning_maps_data": []}]}, "superseded_by": null, "language": "en", "is_atg_ready": 0, "created_by": 4542, "updated_by": 4542, "is_prof_approved": 0, "content_schema_version": 1, "version": 1, "_updated": "Fri, 11 Sep 2020 12:17:14 GMT", "_created": "Fri, 11 Sep 2020 12:17:14 GMT", "_version": 1, "tenant_id": 1, "title": " In the figure, a rectangular block is gradually pushed face down into a liquid. The block has height <math xmlns="http://www.w3.org/1998/Math/MathML">d; on the bottom and top the face area is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">A=8.00 cm2. In the figure, gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ? \\n\\n <img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 150px;" /> <img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 197px;" /> \\n", "id": 6126381, "question_code": "EM6126381-en", "uuid": "d00dfd63-082e-4062-8f1f-e37e7c9f0829", "created_at": "Fri, 11 Sep 2020 12:17:14 GMT", "updated_at": "Fri, 11 Sep 2020 12:17:14 GMT", "_etag": "3027df270a05e67d65b254d9b134bab952cbcd68", "_links": {"self": {"title": "learning_objects", "href": "learning_objects/5f5b6acab4175c979b8cb5af"}}, "_latest_version": 1, "user_name": "reshma awale"}], "_links": {"parent": {"title": "home", "href": "/"}, "self": {"title": "learning_objects", "href": "learning_objects?where={"type":"Question","$or":[{"id":{"$in":[6126381]}},{"question_code":{"$in":[]}}]}&sort=id&embed=true"}}, "_meta": {"page": 1, "max_results": 25, "total": 1}}` const images = string.match(/<img[^>]+src="([^">]+)"/g) const div = document.createElement("div"); div.innerHTML += images.join("<br/>"); // you can use a div in memory const sources = [...div.querySelectorAll("img")].map(img => img.src) console.log(sources) sources.forEach(src => { const url = new URL(src) url.hostname = "yourhost.com"; string = string.split(src).join(url.href); }) console.log(string)

Replace the above forEach with something like this untested code

let counter = sources.length
var saveUrlAndRetrieveNewUrl = async() => {
  while (true) {
    var res = await fetch(`/yourdbstore.php?url=${sources[--counter]}`); // the php will get the image, store it and return new URL
    if (counter = 0) break; // stop
    const data = await res.text();
    const url = new URL(data)
    url.hostname = "yourhost.com";
    string = string.split(src).join(url.href);
  };
  console.log("done");
};
saveUrlAndRetrieveNewUrl();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM