0

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
}
]
}
5
  • Where's your code. Commented Sep 13, 2016 at 14:10
  • The test can have more links or it will be just one, cause in this case you will need to merge the json responses, test: [{href: ""}, {href: ""}] I can help you with the code if you want... Commented Sep 13, 2016 at 14:11
  • @AlexandruOlaru it just one link :) Commented Sep 13, 2016 at 14:30
  • As i see ` 'wp: term'` has two link, maybe as response you will want an array of jsons ? Commented Sep 13, 2016 at 15:16
  • @AlexandruOlaru That could also be great, yes Commented Sep 15, 2016 at 7:04

1 Answer 1

1
  1. You need to parse your JSON first to get the links (maybe put them in an array). (hint: use JSON.stringify() and it becomes a string that you can parse)

  2. Then iterate over each array element and send XHR requests (hint: XMLHttpRequest object). If you want to 'care' for each response, then use xhttp.open(method,url,false) and xhttp.send(). The 'false' will tell that Asynchronous mode is off and so it becomes synchronous. Beware of performance issues now since its synchronous. Read more here

  3. Now create your own JSON object by storing the links in a string and using json.parse(string) to convert it to json

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I hope it helped. Although I understand your case here is to wait for response from each request before proceeding, I strongly suggest you to try implementing your app in an asynchronous manner. Make sure you handle error responses as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.