Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Formatting error

I am getting this error while trying to import this JSON into google bigquery table file-00000000: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. (error code: invalid) JSON parsing error in row starting at position 0 at file: file-00000000. Start of array encountered without start of object. (error code: invalid)

This is the JSON

[{'instrument_token': 11192834, 'average_price': 8463.45, 'last_price': 8471.1, 'last_quantity': 75, 'buy_quantity': 1065150, 'volume': 5545950, 'depth': {'buy': [{'price': 8471.1, 'quantity': 300, 'orders': 131072}, {'price': 8471.0, 'quantity': 300, 'orders': 65536}, {'price': 8470.95, 'quantity': 150, 'orders': 65536}, {'price': 8470.85, 'quantity': 75, 'orders': 65536}, {'price': 8470.7, 'quantity': 225, 'orders': 65536}], 'sell': [{'price': 8471.5, 'quantity': 150, 'orders': 131072}, {'price': 8471.55, 'quantity': 375, 'orders': 327680}, {'price': 8471.8, 'quantity': 1050, 'orders': 65536}, {'price': 8472.0, 'quantity': 1050, 'orders': 327680}, {'price': 8472.1, 'quantity': 150, 'orders': 65536}]}, 'ohlc': {'high': 8484.1, 'close': 8336.45, 'low': 8422.35, 'open': 8432.75}, 'mode': 'quote', 'sell_quantity': 998475, 'tradeable': True, 'change': 1.6151959167271395}]

http://jsonformatter.org/ also gives parse error for this JSON block. Need help understanding where the formatting is wrong - this is the JSON from a rest API

like image 816
Chandan Kumar Avatar asked Dec 05 '22 17:12

Chandan Kumar


2 Answers

This is not valid JSON. JSON uses double quotes, not single quotes. Also, True should be true.

If I had to guess, I would guess that this is Python code being passed off as JSON. :-)

I suspect that even once this is made into correct JSON, it's not the format Google BigQuery is expecting. From https://cloud.google.com/bigquery/data-formats#json_format, it looks like you should have a text file with one JSON object per line. Try just this:

{"mode": "quote", "tradeable": true, "last_quantity": 75, "buy_quantity": 1065150, "depth": {"buy": [{"quantity": 300, "orders": 131072, "price": 8471.1}, {"quantity": 300, "orders": 65536, "price": 8471.0}, {"quantity": 150, "orders": 65536, "price": 8470.95}, {"quantity": 75, "orders": 65536, "price": 8470.85}, {"quantity": 225, "orders": 65536, "price": 8470.7}], "sell": [{"quantity": 150, "orders": 131072, "price": 8471.5}, {"quantity": 375, "orders": 327680, "price": 8471.55}, {"quantity": 1050, "orders": 65536, "price": 8471.8}, {"quantity": 1050, "orders": 327680, "price": 8472.0}, {"quantity": 150, "orders": 65536, "price": 8472.1}]}, "change": 1.6151959167271395, "average_price": 8463.45, "ohlc": {"close": 8336.45, "high": 8484.1, "open": 8432.75, "low": 8422.35}, "instrument_token": 11192834, "last_price": 8471.1, "sell_quantity": 998475, "volume": 5545950}
like image 126
user94559 Avatar answered Dec 21 '22 23:12

user94559


OP has a valid JSON record but that wouldn't work with Biq Query, and here's why:

  1. Google Big Query supports, JSON objects {}, one object per line. Check this out.
  2. This basically means that you cannot supply list [] as json records and expect Big Query to detect it. You must always have one json object per line.

  3. Here's a quick reference to what I am saying.

(Image courtesy: json.org) and there are more.


at last,

I highly recommend you read up the below and check out the link for more information on different forms of JSON structures, read this from the json.org


like image 45
geekidharsh Avatar answered Dec 22 '22 00:12

geekidharsh