0

I have a drupal website where articles are created then output as JSON to a particular link. I am currently trying to parse the JSON and save the title, body etc. of the articles on Parse Core. An example of the JSON output:

[
  {
    "vid": "2",
    "uid": "1",
    "title": "Post 2",
    "log": "",
    "status": "1",
    "comment": "0",
    "promote": "1",
    "sticky": "0",
    "nid": "2",
    "type": "article",
    "language": "und",
    "created": "1435932743",
    "changed": "1436089990",
    "tnid": "0",
    "translate": "0",
    "revision_timestamp": "1436089990",
    "revision_uid": "1",
    "body": {
      "und": [
        {
          "value": "Integer at mi blandit ipsum malesuada consectetur...",
          "summary": "",
          "format": "plain_text",
          "safe_value": "<p>Integer at mi blandit ipsum malesuada consectetur...</p>\n",
          "safe_summary": ""
        }
      ]
    },
    "field_tags": [],
    "field_image": [],
    "name": "uknj",
    "picture": "0",
    "data": "b:0;"
  },
  {
    "vid": "1",
    "uid": "1",
    "title": "Sample Post",
    "log": "",
    "status": "1",
    "comment": "0",
    "promote": "1",
    "sticky": "0",
    "nid": "1",
    "type": "article",
    "language": "und",
    "created": "1435931896",
    "changed": "1436090000",
    "tnid": "0",
    "translate": "0",
    "revision_timestamp": "1436090000",
    "revision_uid": "1",
    "body": {
      "und": [
        {
          "value": "Lorem ipsum dolor sit amet...",
          "summary": "",
          "format": "plain_text",
          "safe_value": "<p>Lorem ipsum dolor sit amet...</p>\n",
          "safe_summary": ""
        }
      ]
    },
    "field_tags": [],
    "field_image": [],
    "name": "uknj",
    "picture": "0",
    "data": "b:0;"
  }
]

I partially based my code on this github. However due to the fact that the body object contains an array, I can not parse it any further and the direct body-value which contains the text I want cannot be saved.

I took a look at this Stackoverflow question and was still unable to solve the problem. An error was being returned Cannot read property 'length' of undefined. It is worth noting that the vid and title are saved successfully.

Furthermore only one of the posts is noted, the one with "vid" : "2", no idea why it doesn't store the other one.

My main.js code:

var _ = require("underscore");
Parse.initialize('xyz', '123');
var Articles = Parse.Object.extend("Articles");
var article = new Articles();

Parse.Cloud.job("ArticleFeed", function(request, response) {
  Parse.Cloud.httpRequest({
        method: 'GET',
        url: 'URL HERE',
        success: function(httpResponse) {
         var data= JSON.parse(httpResponse.text);
           for (var i = 0; i < data[i].body.und.length; i++) {
        article = new Articles(),
        content = data[i];
        article.set("body", content.body.und[0].value);
        article.set("vid", content.vid);
        article.set("title", content.title);
        var epochTime = content.created;
        var newDate = moment.utc(1234567890000);
        article.set("date_created", newDate);

        articles.push(article);
      }

          article.save();

            response.success(httpResponse.text); // This will respond with the contents of the http response
        },
        error: function(httpResponse) {
            console.error('Request failed with response code ' + httpResponse.status);
            response.error('Request failed with response code ' + httpResponse.status);
        }
    });

});

EDIT: This is the correct code excerpt that works, replacing the incorrect for loop above:

  for (var i = 0; i < data[i].body.und.length; i++) {
            article = new Articles(),
            content = data[i];
            article.set("body", content.body.und[0].value);
            article.set("vid", content.vid);
            article.set("title", content.title);

            articles.push(article);
      }
2
  • 1
    Your code is erroring out at this line: for (var i = 0; i < data.length; i++) { Did you intend to use jsonData instead of data? Commented Jul 6, 2015 at 13:37
  • good call, fixed that. Error remains the same. Commented Jul 6, 2015 at 13:41

1 Answer 1

1

Your JSON output is an Array of Objects.

So if your JSON data is contained in a variable, say, "jsonData", then from "jsonData" you can reach the "body" as follow,

jsonData[0].body

Then to get the array inside body you should go like,

jsonData[0].body.und

In your code you are not reaching the arrays properly, hence you are getting the length undefined error.

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

3 Comments

and to get the array inside of und, in particular the value field, would it be jsonData[0].body.und[0].value?
Nvm, answered my own question, that was indeed how to do it. Will update my question to show fully correct code. Thanks.
Yes, you are right, jsonData[0].body.und[0] will give you the 1st object inside the und array (the 0th element), and then, you can call value property of it, by .value. So, jsonData[0].body.und[0].value is correct. Sorry for the late reply, since it was night here, and I went to bed :P

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.