2

Need some assistance configuring this jq command against json type output.

Example testout.out:

{ "_id" : ObjectId("5aaaa017e4b09780301b6c18"), "account" : "abc", "profile" : "catch", "settings" : { "region" : "us-east-1" } }
{ "_id" : ObjectId("5ad894fbe4b0657c569ed5d8"), "account" : "def", "profile" : "test", "settings" : { "region" : "eu-central-1" } }
{ "_id" : ObjectId("5ae127dee4b06990170a0eb4"), "account" : "ght", "profile" : "main", "settings" : { "region" : "us-east-1" } }

I'm trying to sort by region.

Trying this command:

cat testout.out | jq -s -c 'sort_by(.region) |.[]'

Getting this output: parse error: Invalid numeric literal at line 1, column 20

Expecting alphabetical sort on region:

{ "_id" : ObjectId("5ad894fbe4b0657c569ed5d8"), "account" : "def", "profile" : "test", "settings" : { "region" : "eu-central-1" } }
{ "_id" : ObjectId("5aaaa017e4b09780301b6c18"), "account" : "abc", "profile" : "catch", "settings" : { "region" : "us-east-1" } }
{ "_id" : ObjectId("5ae127dee4b06990170a0eb4"), "account" : "ght", "profile" : "main", "settings" : { "region" : "us-east-1" } }
noober
  • 1,427
  • 3
  • 23
  • 36

2 Answers2

1

jq tries to parse values like ObjectId("5aaaa017e4b09780301b6c18") as numeric value and gives you an expected error in terms of JSON validation.

If your testout.out file would be as follows:

{ "_id" : "ObjectId(\"5aaaa017e4b09780301b6c18\")", "account" : "abc", "profile" : "catch", "settings" : { "region" : "us-east-1" } }
{ "_id" : "ObjectId(\"5ad894fbe4b0657c569ed5d8\")", "account" : "def", "profile" : "test", "settings" : { "region" : "eu-central-1" } }
{ "_id" : "ObjectId(\"5ae127dee4b06990170a0eb4\")", "account" : "ght", "profile" : "main", "settings" : { "region" : "us-east-1" } }

you'll be able to perform the needed sorting:

jq -sc 'sort_by(.settings.region)[]' testout.out

The output:

{"_id":"ObjectId(\"5ad894fbe4b0657c569ed5d8\")","account":"def","profile":"test","settings":{"region":"eu-central-1"}}
{"_id":"ObjectId(\"5aaaa017e4b09780301b6c18\")","account":"abc","profile":"catch","settings":{"region":"us-east-1"}}
{"_id":"ObjectId(\"5ae127dee4b06990170a0eb4\")","account":"ght","profile":"main","settings":{"region":"us-east-1"}}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

You could probably get away with this pre-filter to convert the JSON-like objects to valid JSON:

 sed 's/: ObjectId("\([^"]*\)")/: "ObjectId(\1)"/g' 
peak
  • 105,803
  • 17
  • 152
  • 177