dotCMS / core

Headless/Hybrid Content Management System for Enterprises
http://dotcms.com
Other
865 stars 467 forks source link

json.fetch does not handle arrays #1991

Closed mabouza closed 8 years ago

mabouza commented 11 years ago

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.

bradrice commented 11 years ago

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

jtesser commented 11 years ago

@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

bradrice commented 11 years ago

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

On Tue, Apr 23, 2013 at 12:22 PM, Jason Tesser notifications@github.comwrote:

@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 bradrice1@gmail.com ~ “God has promised pardon to your conversion; He has not promised a tomorrow to your delay.” - Augustine

jtesser commented 11 years ago

@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

bradrice commented 11 years ago

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

jtesser commented 11 years ago

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

bradrice commented 11 years ago

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

bradrice commented 11 years ago

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 commented 11 years ago

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

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

cfalzone commented 10 years ago

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
bradrice commented 10 years ago

Thanks

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

On Wed, Jan 15, 2014 at 10:54 AM, Christopher Falzone < notifications@github.com> 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

bradrice1@gmail.com

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

jgambarios commented 8 years ago

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

DeanGonzalez commented 8 years ago

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

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

dotcmsCodeReview commented 8 years ago

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

DeanGonzalez commented 8 years ago

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

shaydotcms commented 8 years ago

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