6

I am using the Drupal 7 releases of the Feeds and FeedsJSONParser modules, but I can't seem to figure out how to configure this to parse the feeds.

I want to parse this feed into their own nodes.

{
    "BTC_LTC": {
        "last": "0.0237",
        "lowestAsk": "0.02383797",
        "highestBid": "0.0237",
        "percentChange": "0.00642712",
        "baseVolume": "5.69603109",
        "quoteVolume": "240.78778654",
        "isFrozen": "0"
    },
    "BTC_NXT": {
        "last": "0.00007000",
        "lowestAsk": "0.00007350",
        "highestBid": "0.00007000",
        "percentChange": "-0.06542056",
        "baseVolume": "0.84850609",
        "quoteVolume": "12014",
        "isFrozen": "0"
    },
    "BTC_NMC": {
        "last": "0.00403",
        "lowestAsk": "0.00489",
        "highestBid": "0.00404001",
        "percentChange": "-0.04952830",
        "baseVolume": "0.27214383",
        "quoteVolume": "64.24716761",
        "isFrozen": "0"
    }
}

I have the following settings for JSONparser, but it doesn't seem to do anything. Would someone be kind enough to take a snapshot of there setup?

screenshot

screenshot

5
  • it looks like your selectors are all default values? Eg. you need to write Selectors for the Title, and all your other fields. You also need a drupal Node with compatible Fields from your expected feed.
    – tenken
    May 16, 2014 at 21:44
  • I think tenken is right. Look at some JSONPath documentation to figure out the needed paths. Here you can read something about that: goessner.net/articles/JsonPath May 17, 2014 at 0:11
  • Yea I just wanted to do an intial test first, which is at least get the title in before proceeding to configure anything else. Just wanted to get the first step down. yeah I'm using the JSON $.[] path option which should technically get the title. May 17, 2014 at 19:49
  • Where should the JSON source be entered? I can not find the place. Is it a URL or is it a piece of text in JSON format?
    – chalo
    Mar 22, 2018 at 22:00
  • I found this site with a tutorial in case someone else is interested in how to configure the import: Tutorial import feeds usin jsonpath
    – chalo
    Mar 23, 2018 at 20:50

3 Answers 3

5
+100

I think you have a problem with the "context", and title query. I've tried this configuration and worked for me.

The context is the first query and in this case just selects all the nodes, the second one takes the value of the key "last" ant put it on the node title.

Then you can play with other querys, to create the nodes, but at least this one works.

enter image description here

3
  • awesome that worked, but how do i get the parent? I want to grab BTC_LTC as the title and then the rest i can do what you have shown. May 21, 2014 at 22:02
  • 1
    As i see, your problem is not with the modules or even drupal it's the FeedsJSONParser that doesn't allow you to parse the name (or key) of the nodes. So if you have access to change the feed, as @paul-m suggest, do a better formated one and use the module, if you don't, parse it with code as the other answer.
    – angoru
    May 22, 2014 at 7:15
  • yea no way to change the feed. writing custom custom wouldnt work as I need to leverage the existing feeds module to have it update the existing nodes once imported and utilize a few other modules that manipulate the current feeds module setup. maybe i can modify the FeedsJSONParser to account for the key value? May 22, 2014 at 15:58
4

Why not just do it custom? Here is an example to get you started. Set your cron on the server to run every hour. This requires entity api

/** implements hook_cron **/
function poloniex_cron(){
 poloniex_get_feed();
}

function poloniex_get_feed(){
 //Get the feed and turn it into json
 $feed = file_get_contents("https://poloniex.com/public?command=returnTicker");
 $feed_json = json_decode($feed);

 foreach($feed_json as $item){
  poloniex_create_node($item);
 }
}

/**$item should be an array from the json feed**/
function poloniex_create_node($item){
  //Create an instance of the node and wrap it
  $node = entity_create('node', array('type' => 'content_type'));
  $node_wrapper = entity_metadata_wrapper('node',$node);

  //Set properties
  $node_wrapper->author = 1; // Admin as the author
  $node_wrapper->title->set("String");  //Title
  $node_wrapper->field_name->set("String"); //Textfield
  $node_wrapper->save();
}
2
  • 1
    I usually do this kind of stuff as a drush command, which allows me to test it independently of cron. Then you can still add this command in crontab.
    – donquixote
    May 18, 2014 at 20:22
  • yes this works perfectly if i just want to import it directly as nodes into a content type, but i also want to be able to leverage other modules that go along with the feeds module like feeds_tamper: drupal.org/project/feeds_tamper -- so doing custom wouldnt work in this setting. May 22, 2014 at 22:05
0

The problem is that the JSON isn't structured in a way to make a easy mapping to a node. If the record were more like:

"record": {"name": "BTC_LTC", "last": "0.0237", etc }

...it would be much easier. You could then use angoru's answer and just parse $.name for a value like BTC_LTC and put that in the title.

The other problem is that by the time you learn enough JPath to extract an individual record and its name, you could have written your own Feeds parser. :-)

I recently had to do something similar, and used Feeds: Meetup as a template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.