Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
i'd like to import feed items to local nodes, from a service that outputs feeds in JSON- can this be done easily?
Comments
Comment #1
alex_b CreditAttribution: alex_b commentedYou'd need to build a JSON parser (see plugins/FeedsCVSParser.inc for an example parser). Should be quick and easy to do. The parser will be a custom parser as the format of the JSON feed is proprietary - correct?
Comment #2
mchelensubscribing
Comment #3
gravelpot CreditAttribution: gravelpot commentedI may end up working on this as I need to import popular queries from a Google custom search engine as nodes, and that is output as JSON.
Comment #4
BenK CreditAttribution: BenK commentedSubscribing...
Comment #5
planet4sale CreditAttribution: planet4sale commentedHi gravelpot,
Did you get anywhere with this?
Code I have borrowed from a couple of sources which I am hoping to use for a JSON parser module:
Comment #6
alex_b CreditAttribution: alex_b commentedGood start.
- Don't use $batch->getFilePath(), but $batch->getRaw(), this will give you the actual content of the document.
- I think its not useful to convert the result of json_decode() into an array. I think we should expect an array of items as input in the first place. If this is not the case, converting doesn't do much good. We should rather bail (throw an exception).
Comment #7
planet4sale CreditAttribution: planet4sale commentedThanks,
That looks like it might be easier to work with (looping through arrays was getting a bit mad here).
Comment #8
twistor CreditAttribution: twistor commentedI do not think it's safe to assume that an array will be the input type. In fact, it's a security flaw in javascript. Not a problem here but, I think that most JSON services should be returning objects; usually with a results attribute. Seeing as this is a custom parser, it depends on the data that is returned.
Comment #9
alex_b CreditAttribution: alex_b commented#6/#8: Feeds is architected to import series of items - which need to be arrays or array-like structures.
If we want to support importing single, unwrapped items, the way to go would be not to convert them (as in #5), but to wrap them into an array to make a single item look like a series of items with one entry.
Comment #10
twistor CreditAttribution: twistor commentedI don't think I explained myself correctly. I was trying to say that the result from a typical JSON service is indeed an array, but it is usually attached to a base object as an attribute named something like 'results'.
Comment #11
gravelpot CreditAttribution: gravelpot commentedThis was my implementation for parsing the JSON feed that a Google Custom Search Engine will give you for your popular search terms. I've rolled it into a very small module that just implements the parser plugin. Hope it's helpful!
Comment #12
xjmTracking.
Comment #13
discursives CreditAttribution: discursives commentedsubscribing
Comment #14
DarrellHQ CreditAttribution: DarrellHQ commentedFollowing
Comment #15
fereira CreditAttribution: fereira commentedI've been thinking about writing a json parser for awhile but never got around to it. Although this version is for Google CSE the basic approach should work pretty well. I have a collection of very simple modules which consume json, use json_decode to transform the string to a php array, then some theming functions to theme the content. One of the issues I've run into is that, for the 5.2.* version of PHP the json_decode function will silently return a null if it couldn't decode the json. The 5.3.* version will return an error code. I ran into some problems with json which contained UTF characters that would blow up json_decode(). It would be a good idea to check the results of the json_decode function before attempting to set times from the array.
Comment #16
crizYes, json_decode() is tricky. I had success with the following:
Comment #17
twistor CreditAttribution: twistor commentedYou might want to check out my new module Feeds JSONPath Parser. The syntax is pretty easy to pick up.
Comment #18
alex_b CreditAttribution: alex_b commentedJust found this JSON document spec: http://tools.ietf.org/html/draft-zyp-json-schema-02 . This seems to be the standard to implement for a useful JSON document parser.
Comment #19
maruan CreditAttribution: maruan commentedEdited:
I am new to feeds, and after reading some more, I see that my earlier question did not belong here, but rather to services.
cheers !
Comment #20
Hanno CreditAttribution: Hanno commentedAs the JSONPath parser is launched, I set this issue on fixed. Please feel free to reopen if there is a need.