Reputation: 53
I have this simple JSON which has a name and a nested info with telephone and address, but now I want my address to show first before telephone. How do I do this with jq?
Sample:
jq . sample.json
{
"name": "john",
"info": {
"telephone": "444-5555",
"address": "1234 Main st"
}
}
{
"name": "jane",
"info": {
"telephone": "222-3333",
"address": "1234 Eagle st"
}
}
Ultimately, I want address before telephone:
{
"name": "john",
"info": {
"address": "1234 Main st"
"telephone": "444-5555",
}
}
So I would do something like this:
jq ". | {name: .name, info: [.info.address, .info.telephone]}" sample.json
But this would give me instead:
{
"name": "john",
"info": [
"1234 Main st",
"444-5555"
]
}
{
"name": "jane",
"info": [
"1234 Eagle st",
"222-3333"
]
}
Upvotes: 1
Views: 41
Reputation: 53
Oops, it was easy as:
jq ". | {name: .name, info: {address: .info.address, telephone: .info.telephone}}" sample.json
Result:
{
"name": "john",
"info": {
"address": "1234 Main st",
"telephone": "444-5555"
}
}
{
"name": "jane",
"info": {
"address": "1234 Eagle st",
"telephone": "222-3333"
}
}
Upvotes: 3