Select Specific Fields From DB Export That Includes Json
I have an export from a DB table that has the following columns:
The external_atribute is on Json format. So the export looks like this:
Which is the most efficient way (since the export has more than 1m lines) to keep only the name and the values from
label6. For example from the above export I would like to keep only:
Edit: I am not sure for the sequence of the fields/variables on the JSON part. Data could be also for example:
Also the fact that some of the values are double quoted, while some are not, is intentional (This is how they appear also on the export).
My understanding until now is that I have to use something that has a JSON parser like Python or jq.
This is what i created on Python and seems that is working as expected:
from __future__ import print_function import sys,json with open(sys.argv, 'r') as file: for line in file: fields = line.split('|') print (fields, json.loads(fields)['label2'], json.loads(fields)['label5'], json.loads(fields)['label6'], sep='|')
Since I am looking for the most efficient way to do this, any comment is more than welcome.
Even if the data are easy to parse, I advise to use a json parser like
jq to extract your json data:
<file jq -rR ' split("|")|[ ., (.|fromjson|(.label2,.label5,.label6)|tostring)]|join("|")'
-r allows jq to accept and display a string as input and output (instead of json data).
split function enable getting all fields into an array that can be indexed with number
The third field is then parsed as json data with the function
fromjson such that the wanted labels are extracted.
All wanted fields are put into an array and
join together with the
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module