komushi
komushi

Reputation: 139

Adding quotes to an json array and add attribute key with jq

This below is my original json:

{
"type": "type1",
"name": "abc",
"features": [
{ "type": "Feature", "properties": { "district": "d1", "district_code": "101", "block_code": "1010070" }},
{ "type": "Feature", "properties": { "district": "d1", "district_code": "101", "block_code": "1010100" }}]
}

My expected result:

[
  {
    "Data": "{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010070\"}}"
   },
  {
    "Data": "{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010100\"}}"
  }
]

So far I am only able to add quotes by:

.features[] | del(.type) | tojson

which will only give me:

"{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010070\"}}"
"{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010100\"}}"

Thanks in advance!

Upvotes: 1

Views: 951

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

jq approach:

jq '.features | map(del(.type) | {Data: tojson})' file.json

Or the same with array construction [...]:

jq '[.features[] | del(.type) | {Data: tojson}]' file.json

The output:

[
  {
    "Data": "{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010070\"}}"
  },
  {
    "Data": "{\"properties\":{\"district\":\"d1\",\"district_code\":\"101\",\"block_code\":\"1010100\"}}"
  }
]

Upvotes: 3

Related Questions