Search code examples
jsonjqstring-literals

jq construct with value strings spanning multiple lines


I am trying to form a JSON construct using jq that should ideally look like below:-

{
  "api_key": "XXXXXXXXXX-7AC9-D655F83B4825",
  "app_guid": "XXXXXXXXXXXXXX",
  "time_start": 1508677200,
  "time_end": 1508763600,
  "traffic": [
    "event"
  ],
  "traffic_including": [
    "unattributed_traffic"
  ],
  "time_zone": "Australia/NSW",
  "delivery_format": "csv",
  "columns_order": [
    "attribution_attribution_action",
    "attribution_campaign",
    "attribution_campaign_id",
    "attribution_creative",
    "attribution_date_adjusted",
    "attribution_date_utc",
    "attribution_matched_by",
    "attribution_matched_to",
    "attribution_network",
    "attribution_network_id",
    "attribution_seconds_since",
    "attribution_site_id",
    "attribution_site_id",
    "attribution_tier",
    "attribution_timestamp",
    "attribution_timestamp_adjusted",
    "attribution_tracker",
    "attribution_tracker_id",
    "attribution_tracker_name",
    "count",
    "custom_dimensions",
    "device_id_adid",
    "device_id_android_id",
    "device_id_custom",
    "device_id_idfa",
    "device_id_idfv",
    "device_id_kochava",
    "device_os",
    "device_type",
    "device_version",
    "dimension_count",
    "dimension_data",
    "dimension_sum",
    "event_name",
    "event_time_registered",
    "geo_city",
    "geo_country",
    "geo_lat",
    "geo_lon",
    "geo_region",
    "identity_link",
    "install_date_adjusted",
    "install_date_utc",
    "install_device_version",
    "install_devices_adid",
    "install_devices_android_id",
    "install_devices_custom",
    "install_devices_email_0",
    "install_devices_email_1",
    "install_devices_idfa",
    "install_devices_ids",
    "install_devices_ip",
    "install_devices_waid",
    "install_matched_by",
    "install_matched_on",
    "install_receipt_status",
    "install_san_original",
    "install_status",
    "request_ip",
    "request_ua",
    "timestamp_adjusted",
    "timestamp_utc"
  ]
}

What I have tried unsuccessfully thus far is below:-

json_construct=$(cat <<EOF
{
"api_key": "6AEC90B5-4169-59AF-7AC9-D655F83B4825",
"app_guid": "komacca-s-rewards-app-au-ios-production-cv8tx71",
"time_start": 1508677200,
"time_end": 1508763600,
"traffic": ["event"],
"traffic_including": ["unattributed_traffic"],
"time_zone": "Australia/NSW",
"delivery_format": "csv"
"columns_order": ["attribution_attribution_action","attribution_campaign","attribution_campaign_id","attribution_creative","attribution_date_adjusted","attribution_date_utc","attribution_matched_by","attribution_matched_to","attributio
network","attribution_network_id","attribution_seconds_since","attribution_site_id","attribution_tier","attribution_timestamp","attribution_timestamp_adjusted","attribution_tracker","attribution_tracker_id","attribution_tracker_name","
unt","custom_dimensions","device_id_adid","device_id_android_id","device_id_custom","device_id_idfa","device_id_idfv","device_id_kochava","device_os","device_type","device_version","dimension_count","dimension_data","dimension_sum","ev
t_name","event_time_registered","geo_city","geo_country","geo_lat","geo_lon","geo_region","identity_link","install_date_adjusted","install_date_utc","install_device_version","install_devices_adid","install_devices_android_id","install_
vices_custom","install_devices_email_0","install_devices_email_1","install_devices_idfa","install_devices_ids","install_devices_ip","install_devices_waid","install_matched_by","install_matched_on","install_receipt_status","install_san_
iginal","install_status","request_ip","request_ua","timestamp_adjusted","timestamp_utc"]
}
EOF)

followed by:-

echo "$json_construct" | jq '.'

I get the following error:-

parse error: Expected separator between values at line 10, column 15

I am guessing it is because of the string literal which spans to multiple lines that jq is unable to parse it.


Solution

  • Your input "JSON" is not valid JSON, as indicated by the error message.

    The first error is that a comma is missing after the key/value pair: "delivery_format": "csv", but there are others -- notably, JSON strings cannot be split across lines. Once you fix the key/value pair problem and the JSON strings that are split incorrectly, jq . will work with your text. (Note that once your input is corrected, the longest JSON string is quite short -- 50 characters or so -- whereas jq has no problems processing strings of length 10^8 quite speedily ...)

    Generally, jq is rather permissive when it comes to JSON-like input, but if you're ever in doubt, it would make sense to use a validator such as the online validator at jsonlint.com

    By the way, the jq FAQ does suggest various ways for handling input that isn't strictly JSON -- see https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json