I have a json objectlike this
{"test": [{"href": "http://sdfsd.fd"}], "test2": [{"href": "http://sdfsd.fd"}]}
What I want to do is interate over all the links in the json, every likn point to another json object. then i want to get the json from the links, and replace the links with that json, so it comes like something like this:
{"test": {somejson}, "test2": {somejson}}
can this be done? right now I tried with a nested for loop, but the loop doesnøt care that the hhtp request hasn't got a response, before continuing in the loop, resulting in nothing is gonna be edited.
EDIT:
my code so far looks like this:
self.buildJSON = function(json) {
var links = [];
for(var post in json){
// console.log(json[post]['_links']);
for(var link in json[post]['_links']){
links.push(json[post]['_links'][link][0]['href']);
}
}
// var regex = /(http[s]?:\/\/)?([^\/\s]+)(.*)/
// for(link in links){
// var match = regex.exec(links[link]);
// var host = match[1]+match[2];
// var path = match[3];
// links[link] = {"host": host, "path": path};
// }
for(link in links){
request(links[link], function(error, response, body){
if (!error && response.statusCode == 200) {
links[link] = body;
}
})
}
console.log(links);
fs.writeFile(self.jsonfile, JSON.stringify(json));
the Json something like this ('_links' is part of a bigger json):
_links: {
self: [
{
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/posts/19"
}
],
collection: [
{
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/posts"
}
],
about: [
{
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/types/post"
}
],
author: [
{
embeddable: true,
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/users/1"
}
],
replies: [
{
embeddable: true,
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/comments?post=19"
}
],
version-history: [
{
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/posts/19/revisions"
}
],
wp:featuredmedia: [
{
embeddable: true,
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/media/130"
}
],
wp:attachment: [
{
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/media?parent=19"
}
],
wp:term: [
{
taxonomy: "category",
embeddable: true,
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/categories?post=19"
},
{
taxonomy: "post_tag",
embeddable: true,
href: "http://wordpress.sutdigselv.dk/wp-json/wp/v2/tags?post=19"
}
],
curies: [
{
name: "wp",
href: "https://api.w.org/{rel}",
templated: true
}
]
}
test: [{href: ""}, {href: ""}]
I can help you with the code if you want...