shubhra
shubhra

Reputation: 782

Change the returned WP Rest API JSON response Format

Is there a way to change the returned wp rest api json format? Instead of an array i'd like the response to be in an object format.

Desired format:

{
    "articles": [
        {
            "id": 160,
            "title": "This is a new post ",
            "slug": "this-is-a-new-post-faf0no",
            "author": 3,
        }
    ]
}

I didn't find any hooks related to this. Any help is appreciated. Thank you.

Upvotes: 1

Views: 794

Answers (1)

macdanet
macdanet

Reputation: 31

You can try to make it an object with

$myObject = json_decode($responseJSON);

and you can take value with

echo $myObject['articles'][0]->title;

with foreach:

foreach($myObject['articles'] as $key => $value) {
    echo $value->title . ", " . $value->slug . "<br>";
}

Upvotes: 2

Related Questions