I'm trying to get all the data from a json file however I have problems to filter out missing objects without one out of two values (EMAIL,JOB_TITLE).
Here's the json :
[{"primary": "JOHN DOE", "attributes": [{"type": "double", "name":"BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "EMAIL", "value":"[email protected]"}, {"type": "string", "name": "JOB_TITLE", "value": "SALESMAN"} ], "aliases": [{"alias": "[email protected]"}]},{"primary": "LORRAINE DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": 456}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "STATUS", "value": "Unavaliable"}, {"type": "string", "name": "EMAIL", "value": "[email protected]"}, {"type": "string", "name": "JOB_TITLE", "value": "SECRETARY"} ], "aliases": [{"alias": "[email protected]"}, {"alias": "[email protected]"}]},{"primary": "JACK DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name":"JOB_TITLE", "value": "OWNER"}], "aliases": [{"alias":"[email protected]"},{"alias": "[email protected]"}]}, {"primary": "NOAH DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "EMAIL", "value": "[email protected]"}], "aliases": [{"alias": "[email protected]"}]}]
The list that I try to create:
"[email protected]": "SALESMAN",
"[email protected]": "SECRETARY"
The code that I used:
echo '[{"primary": "JOHN DOE", "attributes": [{"type": "double", "name":"BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "EMAIL", "value":"[email protected]"}, {"type": "string", "name": "JOB_TITLE", "value": "SALESMAN"} ], "aliases": [{"alias": "[email protected]"}]},{"primary": "LORRAINE DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": 456}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "STATUS", "value": "Unavaliable"}, {"type": "string", "name": "EMAIL", "value": "[email protected]"}, {"type": "string", "name": "JOB_TITLE", "value": "SECRETARY"} ], "aliases": [{"alias": "[email protected]"}, {"alias": "[email protected]"}]},{"primary": "JACK DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name":"JOB_TITLE", "value": "OWNER"}], "aliases": [{"alias":"[email protected]"},{"alias": "[email protected]"}]}, {"primary": "NOAH DOE", "attributes": [{"type": "double", "name": "BUILDING_NUMBER", "value": "123"}, {"type": "double", "name": "FLOOR", "value": 10}, {"type": "string", "name": "EMAIL", "value": "[email protected]"}], "aliases": [{"alias": "[email protected]"}]}]' | jq '.[].attributes[]|select(.name|contains("JOB_TITLE"), contains ("EMAIL"))' | jq .value | sed 'N;s/\n/:/' | sed 's/$/,/'
CodePudding user response:
This turns the attributes array into an object using from_entries
, filters according to the presence of both keys using select
and has
, and extracts the values required using string interpolation:
jq -r '
.[].attributes | from_entries
| select(has("JOB_TITLE") and has("EMAIL"))
| "\"\(.EMAIL)\": \"\(.JOB_TITLE)\""
'
"[email protected]": "SALESMAN"
"[email protected]": "SECRETARY"