How to get a value from a bundle?

I am trying to get the value from a parsed json. But it just returns empty. I tried with and without quotes.

image

It’s not working because your syntax tries to pass ‘x-wp-total’ as if it were an array index.

Instead, use get(map()) to filter to the desired array item and extract its value.

Here’s an example from a very similar GET request. Let’s say we want the value for “cache-control”, which is “private, max-age=0”:

It’s the third item in the array, so we could extract it like this:

But we don’t know whether it’s guaranteed to be the third item. Here’s a more robust way:

  1. Filter to the array item we want. It’s effectively saying “Give me the value whose name is 'cache-control'”. (Of course that would be 'x-wp-total' in your case).

So far, this produces a one-item array:

  1. Not wrong, but we actually want the string/numeric value from inside that item, so wrap the function above in get() with a path of 1:

Voilà!

4 Likes

Thank you so much for explaining this.

I had tried with the map function, but couldn’t get it to work. Your explanation made it crystal clear for me.

1 Like