0
{
    "\u0000*\u0000collection_key": "rows",
    "\u0000*\u0000internal_gapi_mappings": [],
    "cacheHit": true,
    "\u0000*\u0000errorsType": "Google_Service_Bigquery_ErrorProto",
    "\u0000*\u0000errorsDataType": "array",
    "jobComplete": true,
    "\u0000*\u0000jobReferenceType": "Google_Service_Bigquery_JobReference",
    "\u0000*\u0000jobReferenceDataType": "",
    "kind": "bigquery#queryResponse",
    "pageToken": null,
    "\u0000*\u0000rowsType": "Google_Service_Bigquery_TableRow",
    "\u0000*\u0000rowsDataType": "array",
    "\u0000*\u0000schemaType": "Google_Service_Bigquery_TableSchema",
    "\u0000*\u0000schemaDataType": "",
    "totalBytesProcessed": "0",
    "totalRows": "1",
    "\u0000*\u0000modelData": {
        "schema": {
            "fields": [{
                "name": "total",
                "type": "INTEGER",
                "mode": "NULLABLE"
            }]
        },
        "jobReference": {
            "projectId": "qa-bigquery",
            "jobId": "job_y_qXq9mbzrnHzQZf_CUCr8itgmA"
        },
        "rows": [{
            "f": [{
                "v": "666750"
            }]
        }]
    },
    "\u0000*\u0000processed": []
}

I have above output from my script I need to traverse through rows & get value {"v":"666750"}] ? Can you please help ?

2
  • You don't want to decode it? That's the best and easiest way... Commented Apr 28, 2017 at 9:26
  • It might be better to look at how you generate this JSON String or how you call the API that returns it to you. Commented Apr 28, 2017 at 9:49

2 Answers 2

1

This is not particularly elegant, but it does seem to work.

First I removed all the \u0000*\u0000 from the string and then just did a normal json_decode()

$s = '{
    "\u0000*\u0000collection_key": "rows",
    "\u0000*\u0000internal_gapi_mappings": [],
    "cacheHit": true,
    "\u0000*\u0000errorsType": "Google_Service_Bigquery_ErrorProto",
    "\u0000*\u0000errorsDataType": "array",
    "jobComplete": true,
    "\u0000*\u0000jobReferenceType": "Google_Service_Bigquery_JobReference",
    "\u0000*\u0000jobReferenceDataType": "",
    "kind": "bigquery#queryResponse",
    "pageToken": null,
    "\u0000*\u0000rowsType": "Google_Service_Bigquery_TableRow",
    "\u0000*\u0000rowsDataType": "array",
    "\u0000*\u0000schemaType": "Google_Service_Bigquery_TableSchema",
    "\u0000*\u0000schemaDataType": "",
    "totalBytesProcessed": "0",
    "totalRows": "1",
    "\u0000*\u0000modelData": {
        "schema": {
            "fields": [{
                "name": "total",
                "type": "INTEGER",
                "mode": "NULLABLE"
            }]
        },
        "jobReference": {
            "projectId": "qa-bigquery",
            "jobId": "job_y_qXq9mbzrnHzQZf_CUCr8itgmA"
        },
        "rows": [{
            "f": [{
                "v": "666750"
            }]
        }]
    },
    "\u0000*\u0000processed": []
}';

$s1 = str_replace(array('\u0000*\u0000'), '', $s);
$j = json_decode($s1);
//echo json_last_error_msg();
//print_r($j);

// you now have a PHP object as defined by the original string

echo $j->modelData->rows[0]->f[0]->v;

RESULT

666750
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look on json_decode() function.

<?php

$jsonString = '{"*collection_key":"rows","*internal_gapi_mappings":[],"cacheHit":true,"*errorsType":"Google_Service_Bigquery_ErrorProto","*errorsDataType":"array","jobComplete":true,"*jobReferenceType":"Google_Service_Bigquery_JobReference","*jobReferenceDataType":"","kind":"bigquery#queryResponse","pageToken":null,"*rowsType":"Google_Service_Bigquery_TableRow","*rowsDataType":"array","*schemaType":"Google_Service_Bigquery_TableSchema","*schemaDataType":"","totalBytesProcessed":"0","totalRows":"1","*modelData":{"schema":{"fields":[{"name":"total","type":"INTEGER","mode":"NULLABLE"}]},"jobReference":{"projectId":"qa-bigquery","jobId":"job_y_qXq9mbzrnHzQZf_CUCr8itgmA"},"rows":[{"f":[{"v":"666750"}]}]},"*processed":[]}';
$jsonObject = json_decode($jsonString);

var_dump($jsonObject->modelData->rows->f->v)

2 Comments

His problem is (if you test his actual json string) that it will not decode and you get an error The decoded property name is invalid
Yes @RiggsFolly I am getting exception as "Fatal error: Cannot access property started with '\0' in /Applications/XAMPP/xamppfiles/htdocs/BQ.php on line 48"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.