We have a view that needs to export a taxonomy term as JSON along with the content that's associated with the term. So, the structure is like this:
{
events: [{
"title": "Term Title",
"relatedItems": [
{
"title": "Post #1 title"
},
{
"title": "Post #2 title"
}
]
}]
}
There is more information returned about the term and the items than in this simple example, but you can see the structure.
I found that using Views Field View allows me to embed another display of the same view to get the nested structure, as long as the other display also is set to output JSON in "Views API mode" (otherwise you get no output since the other display halts request processing). The only problem is that the top-level escapes the value. Technically, that's the expected behavior -- normally you wouldn't want a value that looks like JSON to throw off the actual JSON wrapper -- but in this case, it would be great if I could just tell the outer display not to escape that one field.
To support this, I assume we'd need to do the following:
- Add a new table to the "JSON" display style settings that presents each of the fields along with a checkbox for "Render raw, without escaping".
- Alter
_views_json_json_encode()to take an additional parameter called$unescaped_field_titlesthat specifies which fields (if any) should not be escaped. The function would need to basically go row-by-row and pull those fields out to avoid them being escaped. Then, after escaping the rest of the content, we'd need to basically go in and inject the raw JSON data in the right place in the data stream. - Alter calls to
_views_json_json_encode()to obtain and pass the list of fields, based on the settings specified in #1.
I know that #2 sounds like an ugly kludge, but I don't see an obvious, convenient way to avoid escaping certain parts of the data passed to json_encode(). Perhaps we could just walk the rows recursively and encode fields more explicitly, piecewise? I suppose that would be less error prone.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | no_quotes_on_json-2468325-12.patch | 1.22 KB | lolandese |
| #11 | no_quotes_on_json-2468325-11.patch | 1.21 KB | lolandese |
| #9 | no_quotes_on_json-2468325-9.patch | 1.8 KB | lolandese |
Comments
Comment #1
guypaddock commentedUpdated issue title so others find this topic when trying to do nested data.
Comment #2
guypaddock commentedHere's a utility function we're using in the interim. We call this from a custom Views JSON style output template, in place of what are normally calls to
_views_json_encode_formatted():A sample template:
Comment #3
guypaddock commentedComment #4
phponwebsites commentedI have created new views in which page, pie chart and attach,ment_1 is avilable. When i try to access that page, it display only first page elements. I want to get all three in views. how to do this?
Comment #5
guypaddock commented@phponwebsites: Please open a different issue. Your request has nothing to do with JSON or this ticket, as far as I can tell.
Comment #6
mauriziopinotti commentedYou may want to have a look at the REST/JSON module as an alternative to Views Datasource:
https://www.drupal.org/project/rest_json
It features out-of-the-box rest APIs and it's primarily meant for devalopers (no UI, configuration in code, a few hooks to alter the default behavior).
Comment #7
guypaddock commentedWe looked at Rest JSON but the output was too verbose for the use case we had. Views Datasource was a lot closer to ideal because we could select just the data we wanted.
Comment #8
studio-days commentedHas there been any developments with this? I'm not a php developer but it just seems like if you could have a format option in the field settings to not include the inverted commas on output.
Not being able to customise nested arrays is a huge drawback with this module as is.
Comment #9
lolandese commentedIf a module uses a field format that provides valid json or an array, it makes sense to pass the data unprocessed and without wrapping quotes.
Provided patch is work in progress (please ignore).
Comment #10
lolandese commentedComment #11
lolandese commentedStill work in progress.
Comment #12
lolandese commentedStill troublesome to get a well formatted Views preview. The output itself is as expected and can be reviewed or used if needed.
Comment #13
marceldeb commentedThis is a very interesting thread. The reason why is that i am implementing the same solution as OP.
However, my nested array does not contain values and only shows '[]'.
The funny thing is that it works in admin preview mode but not on the actual website / front end
Anyone has had the same problem?
Comment #14
katekuratsu commentedSame issue here. My nested array only shows '[]'. It looks fine on admin preview mode.
Comment #15
imclean commentedI've created a small module to do something similar, REST Export Nested. Does that help?
Nesting multiple levels is possible via Views Field View fields.
Comment #16
katekuratsu commentedThe nested array using Views Field View fields works before, but after the new update to 8.5, it returns an empty array on Postman. The view preview mode looks fine.
Comment #17
wlofgren commentedThe function _views_json_json_nested only allows to pass in a single nested field name. We should allow multiple field names to be passed into the function. Below is the modified function that allows multiple field names.