简体   繁体   中英

JSON file parsing using jq

I have a JSON file with such detailes in it(not full file):

{"property": [
    {

 {
      "name": "test1",
      "value": "{\"test_type\":\"jsystem\",\"order\":1,\"test_id\":\"test_01\",\"physical_setup_id\":\"prd_01\",\"timeout\":\"20\"}",
      "own": true
    },
   {
      "name": "test2",
      "value": "{\"test_type\":\"jsystem\",\"order\":2,\"test_id\":\"test_02\",\"physical_setup_id\":\"prd_02\",\"timeout\":\"30\"}",
      "own": true
    },
   {
      "name": "pass",
      "value": "{\"test_type\":\"jsystem\",\"order\":3,\"test_id\":\"test_03\",\"physical_setup_id\":\"prd_01\",\"timeout\":\"15\"}",
      "own": true
    }
 ]
}

And I am trying to extract all the fields that are contain "name test%NUMBER%" I've tried this: cat build.json | jq '.property | .[] | select(.name=="test1")' cat build.json | jq '.property | .[] | select(.name=="test1")' cat build.json | jq '.property | .[] | select(.name=="test1")' - it works OK , but I want to add digital parameter to .name==test. Something like this:

cat build.json | jq '.property | .[] | select(.name=="test'[0-9]'")'

Ans second I need to parse each data under "value" parameter. Like this:

test1:
test_type=jsystem
order=1
test_id=test_01


]
}

With jq 1.5, regular expression support has been added

So assuming property is an array of your test objects (your example is broken and invalid), you could do this to select the objects with names that follow the pattern:

.property | map(select(.name | test("test[0-9]")))

Then for each of those objects, the value appears to be json strings. You would have to parse those out before you could get anything out of those.

.property | map(
    select(.name | test("test[0-9]"))
        | .value |= fromjson
        | { name } + .value
    )

This will give you an array:

[
  {
    "name": "test1",
    "test_type": "jsystem",
    "order": 1,
    "test_id": "test_01",
    "physical_setup_id": "prd_01",
    "timeout": "20"
  },
  {
    "name": "test2",
    "test_type": "jsystem",
    "order": 2,
    "test_id": "test_02",
    "physical_setup_id": "prd_02",
    "timeout": "30"
  }
]

Format it to your liking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM