-2

How do I format this JSON response to return only the "results". It is a returned value from a curl response. This was a full string that was returned.

HTTP/1.0200OKCache-Control: no-cacheContent-Type: application/jsonDate: Thu,
07Jan201618: 33: 12GMT{
  "meta": {
    "code": 200
  },
  "**results**": {
    "national": {
      "goal_completions": 199,
      "sessions": 16251,
      "page_views": 23315,
      "users": 15691,
      "bounce_rate": 18.4784625
    },
    "regions": [
      {
        "region_id": 31,
        "region_name": "South",
        "data": {
          "ga_site_id": null,
          "goal_completions": null,
          "users": null,
          "sessions": null,
          "page_views": null,
          "bounce_rate": null
        }
      },
      {
        "region_id": 10,
        "region_name": "West",
        "data": {
          "ga_site_id": "4",
          "goal_completions": "199",
          "users": "15691",
          "sessions": "16251",
          "page_views": "23315",
          "bounce_rate": "73.91385000"
        }
      },
      {
        "region_id": 35,
        "region_name": "Northeast",
        "data": {
          "ga_site_id": null,
          "goal_completions": null,
          "users": null,
          "sessions": null,
          "page_views": null,
          "bounce_rate": null
        }
      },
      {
        "region_id": 36,
        "region_name": "Midwest",
        "data": {
          "ga_site_id": null,
          "goal_completions": null,
          "users": null,
          "sessions": null,
          "page_views": null,
          "bounce_rate": null
        }
      }
    ]
  }
}

Thanks.

Tiny
  • 27,221
  • 105
  • 339
  • 599
TheGPWorx
  • 857
  • 3
  • 17
  • 37
  • What technology / language are you using? – Matthias Jan 07 '16 at 18:45
  • I don't fully understand the question, that's an HTTP response with a JSON body. What is doing the returning? The only thing I can think of is doing `return response.results` – Sean C Jan 07 '16 at 18:46
  • It is not clear what you are asking here. You are showing a full repsonse including header information. YOu are saying nothing about how you are trying to work with the JSON data (do you just want to extract get date under results property?) or create the JSON (do you want to create the JSON differently so as to not have `meta` node?) Is you question focused on how to create the JSON data structure or how to work with the data structure you have? Or do you just have access to the the raw HTTP responses and are trying to figure out how to separate header information from content? – Mike Brant Jan 07 '16 at 18:46
  • this is a return value from a curl response.. it was returned as a string.. – TheGPWorx Jan 07 '16 at 18:52
  • Possible duplicate of [How to use cURL to get jSON data and decode the data?](http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data) – Mark Amery Jan 08 '16 at 01:05

1 Answers1

1

So in PHP you want to use the json_decode() function on your output like this:

$jsonDecode = json_decode($response);
$onlyResults = $jsonDecode->results;

which you can then use to gather or loop through the rest of the object.

and I'd suggest turning off CURL response headers:

set CURLOPT_HEADER to false.

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59