Problem/Motivation
I'm using the JSON of Views Datasource as a source for an AngularJS app. I'm just basically printing out a list of nodes (default behaviour) Each node has a tag field (taxonomy reference) which can refer to multiple terms. The end result looks something like this (lists 1 node, title & tags field):
{
"nodes" : [
{
"node" : {
"title" : "TEST",
"Tags" : "lorem, ipsum"
}
}
]
}
The problem is that I would like the value of the"Tags" property to be an array instead of a comma separated string since my Angular app needs to loop through those tags a certain point in the flow. I could parse the string into an object myself on the front end side, but then I would be trying to fix something which isn't happening on the backend side.
Desired output:
{
"nodes" : [
{
"node" : {
"title" : "TEST",
"Tags" : ["lorem", "ipsum"]
}
}
]
}
I'm currently using the standard taxonomy term views field to display the tags. I've tried to use the "Taxonomy all terms" views field handler, but that yields the same result. I've toyed with aggregation, relationships, etc. but then I would get duplicate rows (2 entries, 1 per tag) which isn't what I want.
Is their a way which doesn't involve writing a lot of code or a custom views taxonomy handler to do this?
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#5 | 2219741-json_custom_arrays-5.patch | 5.44 KB | Albert Volkman |
#2 | conceptmap_formatting.zip | 1.65 KB | joehudson |
#2 | json_custom_arrays.patch | 8.33 KB | joehudson |
Comments
Comment #1
ziobudda CreditAttribution: ziobudda commentedHi, have you found a solution for this ?
M.
Comment #2
joehudson CreditAttribution: joehudson commentedI created a custom field formatter to take the tid of the taxonomy ref field from views and generate the desired array. I then had to modify the views_json module to allow that array through to the json_encode call.
I attach a patch file for the modified views_json code, and a zip of my simple module with a custom formatter.
The patch also fixes a bug with the bitmask and the character encoding option not being picked up.
Comment #3
yannickooThanks for the patch but could you please convert the tabs to spaces and remove the trailing spaces? Also a variable like
$isjsonready
should be named like$json_ready
(no camel-case) :)Comment #4
anthonylindsay CreditAttribution: anthonylindsay at Annertech commentedSo understandably enough, with all the code changes over the last two years, that patch applies about as cleanly as last month's dish-cloth dipped in mud.
So I'm going to close this due to inactivity. @joehudson: if you wanna re-roll your extensive patch for the current dev I'm happy to give it another go.
Comment #5
Albert Volkman CreditAttribution: Albert Volkman commentedI attempted to bring this patch up to date, but it doesn't seem to work.
Comment #6
B.Friddy CreditAttribution: B.Friddy commentedHi Albert, just wondering if you happen to know where you think things need to altered to make your patch work? I'm very interested in having this implemented as well.
Thanks
Comment #7
B.Friddy CreditAttribution: B.Friddy commentedThis isn't ideal, but I'm manipulating the output after the fact and saving it to a new .json file
Here is my code if anyone is interested.
$url = 'feed.json';
$jsonString = file_get_contents($url);
$data = json_decode($jsonString, true);
foreach($data['places'] as &$value)
{
//Create array from tag ids
$value['tags'] = explode(",",$value['tags']);
//Trim the whitespace
$value['tags'] = array_map('trim',$value['tags']);
}
//We don't want to escape slashes (for my particular purposes)
$newJsonString = json_encode($data,JSON_UNESCAPED_SLASHES);
file_put_contents('updated.json', $newJsonString);