1

I have a bash script which takes in query and generates the json. I am looking to generate the new json format as

{ "symbols" : "my_name"
  "nid" : "MyName"
}

Could you please help me in modifying this query

QUERY='SELECT row_to_json(o) FROM (SELECT nid , nid(????) as symbols FROM batch_types ORDER BY id::integer) AS o';
echo $QUERY | psql -h localhost -U report mcore -t $@ | jq -s '.' > generated/enum/batch_type.json

I am expecting some sed after jq as jq -s '.' | sed "...".

I am getting data from PostgreSQL.

Example:

Output from sql could be `

[
 {"name" : "Xml"
  "symbol" : "Xml"
 },
{ "name": "Java",
  "symbols": "Java"
}
]

to

[
 {"name" : "Xml"
  "symbol" : "xml"
 },
{ "name": "Java",
  "symbols": "java"
}
]
S-Man
  • 22,521
  • 7
  • 40
  • 63
Ani
  • 463
  • 4
  • 20
  • With GNU sed: `sed 's/.*/\L&/'` – SLePort Jul 20 '16 at 05:53
  • as mentioned, want to get snake case and not all lower case using sed. – Ani Jul 20 '16 at 10:02
  • [edit] your question to include **clear** concise, testable sample input and expected output. Right now it looks like you want to change Xml to xml and Java to java in some parts of your file but idk what that has to do with snake case or the first sample json so your question ie very unclear and your posted sample input/output (assuming that's what it is) doesn't help with that. – Ed Morton Jul 20 '16 at 18:26

2 Answers2

1

I would prefer awk

awk 'BEGIN{RS="^$"}{print tolower($0)}' >outfile

awk [ manpage ] says:

tolower(str)
Return a copy of the string str, with all the ppercase characters in str translated to their corresponding lowercase counterparts.Non-alphabetic characters are left unchanged.

Edit

If you're trying to convert only the "symbols" value to lowercase, use the below :

...jq -s '.' |  awk '/^\{*[[:blank:]]*"symbol[s]?"[[:blank:]]*:/{
$0=tolower($0);}1'' >outfile

Output

[
 {"name" : "Xml"
  "symbol" : "xml"
 },
{ "name": "Java",
  "symbols": "java"
}
]

Sidenote: ? with awk-regex is for utmost one match which would match either symbol or symbols in this case.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • could you please elaborate how is it working as I only need to generate symbols field in that way...thanks – Ani Jul 20 '16 at 05:28
  • @AnimeshJain : This information was not given in the question, so please update the question. Give possible input and expected output. See the update – sjsam Jul 20 '16 at 05:40
  • Thanks...but above seems to be not working...may be because of variable spaces ?? – Ani Jul 20 '16 at 05:46
  • @AnimeshJain : See the edit in the second part of the answer – sjsam Jul 20 '16 at 05:56
  • Not working when I have like DataFeed which I want as data_feed – Ani Jul 20 '16 at 09:36
  • Where is the above point mentioned in the question? Should I divine and find that out? – sjsam Jul 20 '16 at 10:41
0

I was able to get using sed -r '/symbol/ s/([a-z])([A-Z])/\1_\L\2/g' | awk '/^\{*[[:blank:]]*"symbol[s]?"[[:blank:]]*:/{ $0=tolower($0);}1'''

Thanks everyone for your efforts.

Ani
  • 463
  • 4
  • 20