'JSON API format in API-Platform

First of all I want to implement JSON API. I follow tutorial on api platform and just like in example create entities and response is like

{
    "links": {
        "self": "/api/books"
    },
    "meta": {
        "totalItems": 1,
        "itemsPerPage": 30,
        "currentPage": 1
    },
    "data": [
        {
            "id": "/api/books/1",
            "type": "Book",
            "attributes": {
                "isbn": "9781782164104",
                "title": "Persistence in PHP with the Doctrine ORM",
                "description": "This book is designed for PHP developers and architects who want to modernize their skills through better understanding of Persistence and ORM.",
                "author": "Kévin Dunglas",
                "publicationDate": "2013-12-01T00:00:00+01:00",
                "_id": 1
            },
            "relationships": {
                "reviews": {
                    "data": [
                        {
                            "type": "Review",
                            "id": "/api/reviews/1"
                        }
                    ]
                }
            }
        }
    ]
}

My api_platform.yaml config

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    formats:
        jsonapi:
            mime_types: ['application/vnd.api+json']

So i have problem with id filed in data. I get id fields in format api/entityName/id but I just want to get number(string), just like in JSON API. So is there some configuration that i miss or is any way to achieve that



Solution 1:[1]

It was discussed here. You need to use Normalizer or create custom getter.

Solution 2:[2]

All you need is to send

Accept: application/json

on request header.

Solution 3:[3]

You can use mapply to create a logical matrix of matches and then use it to subset df1.

Test data.

set.seed(2022)
df1 <- data.frame(col1 = letters[1:10], col2 = 1:10, col3 = 11:20)
df2 <- data.frame(col1 = sample(letters[1:10], 4), 
                  col2 = sample(1:10, 4), col3 = 11:14)

Here I start by putting the columns in a vector, it simplifies the code.

cols <- c("col1", "col2")
(i <- mapply(\(x, y) x %in% y, df1[cols], df2[cols]))
#       col1  col2
# [1,] FALSE FALSE
# [2,] FALSE FALSE
# [3,]  TRUE FALSE
# [4,]  TRUE  TRUE
# [5,] FALSE FALSE
# [6,]  TRUE  TRUE
# [7,]  TRUE  TRUE
# [8,] FALSE FALSE
# [9,] FALSE  TRUE
#[10,] FALSE FALSE

Now subset. The question is not very clear on which of the following is asked for.

# at least one column match
j <- rowSums(i) > 0L
df1[j, ]
#  col1 col2 col3
#3    c    3   13
#4    d    4   14
#6    f    6   16
#7    g    7   17
#9    i    9   19

# all columns match
k <- rowSums(i) == length(cols)
df1[k, ]
#  col1 col2 col3
#4    d    4   14
#6    f    6   16
#7    g    7   17

Solution 4:[4]

I think just doing a merge() by the two columns of interest get you what you need. You can then subset the merged output to just columns from the original data.frame. This would return only rows of your query data.frame where col1 and col2 match their cognate values in the reference data.frame. Please clarify if that's NOT your goal.

# simulate two DFs with some common values in col1 and col2
x <- data.frame(col1 = LETTERS[1:5],
                col2 = 1:5,
                col3 = runif(5))

y <- data.frame(col1 = LETTERS[4:8],
                col2 = 4:8,
                col3 = runif(5))

x
#>   col1 col2      col3
#> 1    A    1 0.4306611
#> 2    B    2 0.7149893
#> 3    C    3 0.2808990
#> 4    D    4 0.4383580
#> 5    E    5 0.1372991
y
#>   col1 col2       col3
#> 1    D    4 0.40191250
#> 2    E    5 0.94833538
#> 3    F    6 0.85608320
#> 4    G    7 0.05758958
#> 5    H    8 0.29011770

# merge without adding .x suffix to col3 from x
# then subset to only keep columns from x
merge(x, y, 
      by = c("col1", "col2"), 
      suffixes = c("", ".drop"))[,1:ncol(x)]
#>   col1 col2      col3
#> 1    D    4 0.4383580
#> 2    E    5 0.1372991

Created on 2022-01-08 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0TshEL_n1ck
Solution 2 Alp Altunel
Solution 3 Rui Barradas
Solution 4