wahiggins3
wahiggins3

Reputation: 3

How to loop through JSON objects to extract specific key values with jq

I am new to Bash scripting and jq. I am trying to extract the key value pairs name and transcription.normalized from a JSON object.

I have learned how to get a list of all the values from name and normalized separately but it is not really what I am looking for.

cat submission.json | jq '.documents[] .pages[] .fields[] .name, .documents[] .pages[] .fields[] .transcription.normalized'

I am wondering if I need to perform some sort of loop but just not sure. I really want a single script that pulls those 2 fields in a format that I can easily dump to a CSV file.

This is the example of what the JSON looks like.

{
  "id": 1,
  "state": "complete",
  "substate": null,
  "exceptions": [],
  "name": "Sender Account Number",
  "output_name": null,
  "field_definition_attributes": {
    "required": false,
    "data_type": "Account Number",
    "multiline": false,
    "consensus_required": false,
    "supervision_override": null
  },
  "transcription": {
    "raw": "1685-0441-1",
    "normalized": "168504411",
    "source": "machine_transcription",
    "data_deleted": false,
    "user_transcribed": null,
    "row_index": null
  },
  "field_image_url": "/api/v4/image/be167a88-9d1d-43bc-82b2-3d96d8c06656?start_x=0.3110429607297866&start_y=0.1052441592299208&end_x=0.5696909842243418&end_y=0.16043316955780607"
}

Upvotes: 0

Views: 669

Answers (1)

oguz ismail
oguz ismail

Reputation: 50805

You don't need a loop. And jq can produce CSV out of arrays.

jq -r '.documents[].pages[].fields[] | [.name, .transcription.normalized] | @csv' file

Upvotes: 1

Related Questions