core icon indicating copy to clipboard operation
core copied to clipboard

json.fetch does not handle arrays

Open mabouza opened this issue 13 years ago • 16 comments

The JSONTool that ships with dotCMS is not able to deal with JSON arrays, e.g.

[
{
id: "1",
title: "First Title"
},
{
id: "2",
title: "Second Title"
},
{
id: "3",
title: "Third Title"
}
]

You have to wrap the list as a json property in the string which is clunky. Reason is that JSON arrays are not really valid json objects and fail json validation, .e.g. jsonlint.com, but the fact is most everyone is returning them via their web services and so we should handle.

Additionally, it would be very nice if we could provide a way for users to pass Headers to allow for basic auth and other header properties to the tool.

mabouza avatar Feb 02 '13 05:02 mabouza

Is there any work being done on this one, or is there a workaround to get json formatted as an array?

bradrice avatar Apr 23 '13 16:04 bradrice

@bradrice it works with some JSON but JSON formatted with the example above doesn't work for the array. We will pick off eventually but haven't yet

jtesser avatar Apr 23 '13 16:04 jtesser

Unfortunately, that is the format of the json I need to pull in.

On Tue, Apr 23, 2013 at 12:22 PM, Jason Tesser [email protected]:

@bradrice https://github.com/bradrice it works with some JSON but JSON formatted with the example above doesn't work for the array. We will pick off eventually but haven't yet

— Reply to this email directly or view it on GitHubhttps://github.com/dotCMS/dotCMS/issues/1991#issuecomment-16869026 .

Brad Rice [email protected] ~ “God has promised pardon to your conversion; He has not promised a tomorrow to your delay.” - Augustine

bradrice avatar Apr 23 '13 16:04 bradrice

@bradrice @mabouza using the following code


#set($fArray = $json.fetch("http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback").ResultSet.Result)
 #foreach( $product in $fArray )
$product.symbol
#end

  1. I got 10 elements on our code here.
  2. The JSON object coming here has the below structure :

ResultSet :

    Query : Yahoo

    Result : [

            Symbol: value, name: value, exch: value, type: value, . . . .,

            Symbol: value, name: value, exch: value, type: value, . . . .,

            Symbol: value, name: value, exch: value, type: value, . . . .,

            Symbol: value, name: value, exch: value, type: value, . . . .

            ]

In order to fetch the array elements from the above JSON Object,

we need to dig into JSONObject -> ResultSet -> Result -> ArrayElement

jtesser avatar Jun 06 '13 14:06 jtesser

that isn't the format that I was having trouble with. Mine was a top level array.

This is the format I only get the first result from fetch: https://gist.github.com/bradrice/21b9eea4916c6ed409f1

bradrice avatar Jun 06 '13 15:06 bradrice

@bradrice You'll have to provide a URL with an example that fails.

jtesser avatar Jun 06 '13 15:06 jtesser

Here is the page using that sample array I showed you in the gist.

http://www.uakron.edu/test/test-json.dot

Here is the code on that page:

<!doctype html>

Test Json Pull

Test a json fetch

#set($fArray = $json.fetch("http://www.uakron.edu/test/test.json"))

Array: $fArray

    #foreach($el in $fArray)
  • $el
  • #end
​

bradrice avatar Jun 06 '13 16:06 bradrice

Sorry, that code didn't show correctly. Here is s a gist with my code: https://gist.github.com/bradrice/fb4e064ec60b6d03d77b

The json is here: http://www.uakron.edu/test/test.json

I used jsonlint.org to validate my json.

bradrice avatar Jun 06 '13 18:06 bradrice

I noticed, I am not the only one who has noticed this bug:

http://forumarchive.dotcms.com/JSON-array-td5710829.html

bradrice avatar Jun 06 '13 18:06 bradrice

Confirmed that this format of JSON does not work with the json tool. A workaround:

So I think the json tool expects things to start with objects. You should be able to do a read first and then surround it, something like:

#set($jsonData = $import.read("http://www.uakron.edu/test/test.json"))
#set($jsonData = "{data:${jsonData}}")
#set($jsonObject = $json.generate($jsonData))
#foreach($item in $jsonObject.data)
  $item.id - $item.title <br />
#end

cfalzone avatar Jan 15 '14 15:01 cfalzone

Thanks

Yes, but it would be nice if it just worked right.

On Wed, Jan 15, 2014 at 10:54 AM, Christopher Falzone < [email protected]> wrote:

Confirmed that this format of JSON does not work with the json tool. A workaround:

So I think the json tool expects things to start with objects. You should be able to do a read first and then surround it, something like:

#set($jsonData = $import.read("http://www.uakron.edu/test/test.json")) #set($jsonData = "{data:${jsonData}}") #set($jsonObject = $json.generate($jsonData)) #foreach($item in $jsonObject.data) $item.id - $item.title

#end

— Reply to this email directly or view it on GitHubhttps://github.com/dotCMS/dotCMS/issues/1991#issuecomment-32374046 .

Brad Rice

[email protected]

“Be curious. Read widely. Try new things. What people call intelligence just boils down to curiosity.”

  • Aaron Swartz

bradrice avatar Jan 15 '14 16:01 bradrice

PR: https://github.com/dotCMS/core/pull/9121

jgambarios avatar May 27 '16 16:05 jgambarios

Hey Shay, I just tested using this example that Will gave me... working

https://gist.github.com/DeanGonzalez/721b6aadcf23ca85f31603bd443836b3

DeanGonzalez avatar Jun 01 '16 19:06 DeanGonzalez

We need to test that our non-array json pulls still work as well.

dotcmsCodeReview avatar Jun 01 '16 19:06 dotcmsCodeReview

Tested and see John already documented here during 3.3 release: http://authoring.dotcms.com/docs/latest/jsontool#Iteration

DeanGonzalez avatar Jun 02 '16 15:06 DeanGonzalez

Passed QA. Array and non-array json pulls are working. Tool allows to pass headers Tested locally in master-3.6 // Mac OS 10.10 // MySQL 5.6.23 // Firefox

shaydotcms avatar Jun 06 '16 13:06 shaydotcms