Skip to content

BigQuery: "Failed to parse JSON: No active field found" when using load_table_from_json() #9203

Closed

Description

@jacques-

On Mac (Python 3.7.4 & google-cloud-bigquery==1.19.0) I'm using the following code:

from google.cloud import bigquery

test_ndjson = '{"name": "bob"}'
client = bigquery.Client()
table_ref = client.dataset('my_ds').table('test')
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
job_config.autodetect = True
job = client.load_table_from_json(test_ndjson, table_ref, job_config=job_config)
job.result()  # Waits for table load to complete.

I get:

google.api_core.exceptions.BadRequest: 400 Error while reading data, error message: Failed to parse JSON: No active field found.; ParsedString returned false

When I do exactly the same thing using the bq command it works as expected:

echo '{"name": "bob"}' > data.json
bq load --apilog '' --source_format=NEWLINE_DELIMITED_JSON --autodetect=True myproject\:my_ds.test ./data.json

Activity

  1. added
    api: bigquery
    type: question
    on Sep 11, 2019
  2. plamut commented on Sep 11, 2019

    @plamut
    Contributor

    Confirmed, I was able to reproduce the issue with the posted code (thanks @jacques- ).

  3. added
    priority: p2
    type: bug
    and removed
    type: question
    on Sep 11, 2019
  4. plamut commented on Sep 11, 2019

    @plamut
    Contributor

    Actually, the type of test_ndjson is incorrect. It should be an iterable of dictionaries (docs). Changing test_ndjson to the following will work:

    test_ndjson = [{"name": "bob"}]

    @tswast I suppose it is reasonable to expect that passing in a JSON string to the method should work, too - let's add support for that?
    (re-classifying as feature request, and will update the description if confirmed)

  5. added
    type: feature request
    and removed
    priority: p2
    type: bug
    on Sep 11, 2019
  6. self-assigned this
    on Sep 12, 2019
  7. tswast commented on Sep 12, 2019

    @tswast
    Contributor

    This feels a bit redundant with the load_table_from_file method, since really we just want to pass the bytes through to the resumable upload in this case. Maybe we need a load_table_from_string / load_table_from_bytes method?

  8. plamut commented on Sep 12, 2019

    @plamut
    Contributor

    Have there been feature requests for this in the past?
    Not sure if we really need a *_from_string version of the method, since it is straightforward to wrap string data into a binary file-like object, and pass it to load_table_from_file()...

  9. tswast commented on Sep 12, 2019

    @tswast
    Contributor

    I agree that *_from_string is probably not necessary. A few code samples on how to wrap a string in a BytesIO / StringIO object would likely suffice.

  10. added
    type: docs
    and removed
    type: feature request
    on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigquerytype: docs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions