0

I'm using Kimono to extract some data and create an API:

{
  "name": "site update",
  "count": 4,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Sun Feb 07 2016 05:13:26 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9418/title-1/",
          "text": "Title 1"
        },
        "pubDate": "February 6, 2016",
        "index": 1,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9422/title-2/",
          "text": "Title 2"
        },
        "pubDate": "February 6, 2016",
        "index": 2,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9358/title-3/",
          "text": "Title 3"
        },
        "pubDate": "February 5, 2016",
        "index": 3,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9419/title-4/",
          "text": "Title 4"
        },
        "pubDate": "February 5, 2016",
        "index": 4,
        "url": "http://www.tvtrailers.com/home/"
      }
    ]
  }
}

I'm trying to GET the value of href inside the title element, then explode the string to obtain the id number (9418, 9422, 9358, 9419 on the code above) and create a new property with just the id number.

Or, if is not possible to create a new property, then I would like to just replace all the href string and keep the id number instead of the full href url.

Here is the code I'm using: -Not working

function getpost_number(data) { 
    var post_number = 0; 
    for(var href in data.results) { 
        data.results[href].forEach(function(row) { 
            var parts = row.split("/"); 
            console.log(parts[5]+parts[6]); 
        }); 
    }; 
    data.post_number = post_number; 
    return data; 
}

Result:

{
  "error": "Bad Request",
  "message": "Your function failed to evaluate because of Error: Object object has no method 'split'"
}

Also the code inspector inside kimono had 2 warnings:

On line 7: Don't make functions within a loop.
On line 8: Unnecessary semicolon

I appreciate any help and directions to figure out what's wrong with the code above, thank you.

Addendum - new attempt

Here is the updated function I'm using with the code provided by Trincot on the comments below:

function addPostNumbers(data) {
    for(var collection in data.results) {
        data.results[collection].forEach(function(row) {
            if (parts = row.title.href.match(/\/(\d+)\//)) {
                row.title.post_number = parts[1];
            }
        });
    }
}

Output:

{
  "error": "Bad Request",
  "message": "Your function failed to evaluate because of Error: parts is not defined"
}

Kimono Inspector warnings

line 5: Assignment in conditional expression.
line 8: Don't make functions within a loop.

trincot
  • 317,000
  • 35
  • 244
  • 286
Peter
  • 79
  • 12
  • what does console.log(row) look like? – Gavriel Feb 06 '16 at 22:59
  • Your data is malformed: there are more closing braces than opening. Also, there are some curly quotes there, which are not valid JSON. Can you please correct? – trincot Feb 06 '16 at 23:12
  • I fixed the curly quotes issue for you. But the braces I don't know how they should be, I expect some opening ones are missing.... – trincot Feb 07 '16 at 00:02
  • I saw your update of the question. I fixed the curly quotes again (don't use Word or disable the automatic curly quotes feature in it). Also, it is important to keep your original code attempt in the question: otherwise the question will become too different from its original. I added an "Addendum" title to clearly indicate the edit you made that includes new code. – trincot Feb 07 '16 at 09:32

2 Answers2

0

Your row is going to look like this:

{
  "title": {
    "href": "http://www.tvtrailers.com/scenes/view/id/9358/title/“,
    "text": "Title 1"
  },
  "pubDate": "February 5, 2016",
  },
  "index": 1,
  "url": "http://www.tvtrailers.com/videos/thismonth/bydate/"
}

Which means you want to dig deeper and split on row.title.href

Also, I'm not sure which pieces your are hoping to retrieve, but parts[5] will equal "id" and parts[6] will equal 9358. That is because the // after http: will create an empty item between "http:" and "www.tvtrailers.com".

In other words you split array will look like this: ["http:", "", "www.tvtrailers.com", "scenes", "view", "id", "9358", "title", ""]

Brandon Poe
  • 461
  • 4
  • 10
  • Thank you very much for your help... as an inexperienced user I didn't publish the full data the first time because I thought it was too much unnecessary data to publish.. I apologize. I already updated the warning, the data and the results of the function suggested, I really appreciate your help. Thank you. – Peter Feb 07 '16 at 06:08
0

The data you provided originally was not valid JSON, as it had more closing braces than opening ones, and it had some curly double quotes. This was fixed in your question later.

Here is a function that will add the post_number property in the title objects, provided that the href property contains a folder name that is numerical.

When you run this snippet, it will output the result (JSON) with the additional properties:

function addPostNumbers(data) {
    var collection, rows, i, parts;
    for (collection in data.results) {
        var rows = data.results[collection];
        for (i=0; i<rows.length; i++) {
            parts = rows[i].title.href.match(/\/(\d+)\//);
            if (parts) {
                rows[i].title.post_number = parts[1];
            }
        }
    }
    return data;
}

// test data
var data = {
  "name": "site update",
  "count": 4,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Sun Feb 07 2016 05:13:26 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9418/title-1/",
          "text": "Title 1"
        },
        "pubDate": "February 6, 2016",
        "index": 1,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9422/title-2/",
          "text": "Title 2"
        },
        "pubDate": "February 6, 2016",
        "index": 2,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9358/title-3/",
          "text": "Title 3"
        },
        "pubDate": "February 5, 2016",
        "index": 3,
        "url": "http://www.tvtrailers.com/home/"
      },
      {
        "title": {
          "href": "http://www.tvtrailers.com/scenes/view/id/9419/title-4/",
          "text": "Title 4"
        },
        "pubDate": "February 5, 2016",
        "index": 4,
        "url": "http://www.tvtrailers.com/home/"
      }
    ]
  }
};

// don't add this to your code. Kimono will do this (I suppose):
addPostNumbers(data);

// write result in document, don't add this in your own code
document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');

Concerning the warnings you received:

On line 7: Don't make functions within a loop.

You can ignore this warning. This is intended to avoid that the runtime has to create a function again and again, because it is defined within a loop. Here it concerns the forEach callback function, where the forEach call appears in a loop itself. However, with the forEach construct most runtimes will parse this efficiently.

On line 8: Unnecessary semicolon

This concerns the semi-colon after the closing brace of your for-loop. You should remove that one.

Addendum - feed back

You tried a previous version of my code (present now in your question) and listed some problems raised by Kimono, which seems to have its own Javascript parser and to apply stricter rules than the browser:

line 5: Assignment in conditional expression.

I have updated the above code, moving the assignment out of the conditional expression.

line 8: Don't make functions within a loop.

I had written earlier that this warning can be ignored, but now I have replaced the foreach loop as a standard for loop, so you should not get this warning any more.

line 16: document.write can be a form of eval

The calls to the functions addPostNumbers and document.write are only in my snippet to demo the solution. That part of the code is not intended to be used in your code.

"message": "Your function failed to evaluate because of Error: parts is not defined"

I added a var statement in the code above to avoid this.

I also added a return statement, as Kimono might need that as well, I don't know.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you very much! I tried your kindly suggested code on Kimono but still not working, also because my poor knowledge on PHP I'm not sure if I'm following your instructions. This time I updated the data (full) and the error codes. I really appreciate your help, thank you very much @trincot – Peter Feb 07 '16 at 06:05
  • Thank you very much @trincot it worked and you are a genius and very nice person to take the time to help me, really THANK YOU! It was a little problem.. when I run the code I receive a error: parts it's not defined... I added ' var ' before parts and done .. it works! – Peter Feb 07 '16 at 10:45
  • Great, I added that change in my answer just to have it complete. Note that since there is a `var` statement at the top, it is nicer to add **parts** in there, than to add another `var`. It is a matter of taste though. – trincot Feb 07 '16 at 10:49