json - How to reformat and transform the output of jq? -
i have json string below:
[{ "appid": "server1", "username": "bhavik", "scaleapp": { "imagename": "${data}/build-server:1", "internalpath": "/", "volumesfrom": [ "${data}/buildtype-mock:219", "${data}/buildtype-se:543-1.0.5-v30.2-ga" ] } }, { "appid": "server2", "username": "rajiv", "scaleapp": { "imagename": "${data}/build-server:159", "internalpath": "/", "volumesfrom": [ "${data}/buildtype-mock:218", "${data}/buildtype-se:540-1.0.5-v30.1-ga", "${data}/buildtype-nodejs:42", "${data}/buildtype-flogo:682" ] } } ]
i need output below:
{ "imagename" : "build-server:159", "volumesfrom" : [ "buildtype-mock:218", "buildtype-se:540-1.0.5-v30.1-ga", "buildtype-nodejs:42", "buildtype-flogo:682" ] }
i trying achieve jq. using following command:
jq '.[] | { imagename: .scaleapp.imagename,volumesfrom: .scaleapp.volumesfrom } ' data.json
following output:
{ "imagename": "${data}/build-server:159", "volumesfrom": [ "${data}/buildtype-mock:218", "${data}/buildtype-se:540-1.0.5-v30.1-ga", "${data}/buildtype-nodejs:42", "${data}/buildtype-flogo:682" ] }
i not getting proper format want, please me out.
here solution original problem. if following filter in filter.jq
.scaleapp | {imagename, volumesfrom} | .imagename |= .[8:] | .volumesfrom[] |= .[8:]
and following (original) sample data
{ "appid": "server1", "username": "bhavik", "scaleapp": { "imagename": "${data}/build-server:159", "internalpath": "/", "volumesfrom": [ "${data}/buildtype-mock:218", "${data}/buildtype-se:540-1.0.5-v30.1-ga", "${data}/buildtype-nodejs:42", "${data}/buildtype-flogo:682" ] } }
is in data.json
command
$ jq -m -f filter.jq data.json
produces
{ "imagename": "build-server:159", "volumesfrom": [ "buildtype-mock:218", "buildtype-se:540-1.0.5-v30.1-ga", "buildtype-nodejs:42", "buildtype-flogo:682" ] }
here solution revised problem. assuming following sample data in data.json
[ { "appid": "server1", "username": "bhavik", "scaleapp": { "imagename": "${data}/build-server:1", "internalpath": "/", "volumesfrom": [ "${data}/buildtype-mock:219", "${data}/buildtype-se:543-1.0.5-v30.2-ga" ] } }, { "appid": "server2", "username": "rajiv", "scaleapp": { "imagename": "${data}/build-server:159", "internalpath": "/", "volumesfrom": [ "${data}/buildtype-mock:218", "${data}/buildtype-se:540-1.0.5-v30.1-ga", "${data}/buildtype-nodejs:42", "${data}/buildtype-flogo:682" ] } } ]
this filter.jq
map( .scaleapp | {imagename, volumesfrom} | .imagename |= .[8:] | .volumesfrom[] |= .[8:] )
produces
[ { "imagename": "build-server:1", "volumesfrom": [ "buildtype-mock:219", "buildtype-se:543-1.0.5-v30.2-ga" ] }, { "imagename": "build-server:159", "volumesfrom": [ "buildtype-mock:218", "buildtype-se:540-1.0.5-v30.1-ga", "buildtype-nodejs:42", "buildtype-flogo:682" ] } ]
which little more asked filter.jq
map( select(.appid == "server2") | .scaleapp | {imagename, volumesfrom} | .imagename |= .[8:] | .volumesfrom[] |= .[8:] )[]
produces only
{ "imagename": "build-server:159", "volumesfrom": [ "buildtype-mock:218", "buildtype-se:540-1.0.5-v30.1-ga", "buildtype-nodejs:42", "buildtype-flogo:682" ] }
for .appid == "server2"
Comments
Post a Comment