0

I would like to get first objects (don't know if it's the right name) of my json file that is huge (more than 120k lines), so I can't parse it manually.

Format is like this :

"datanode": [
    {
        "isWhitelisted": true,
        "metricname": "write_time",
        "seriesStartTime": 1542037566944,
        "supportsAggregation": true
    },
    {
        "isWhitelisted": true,
        "metricname": "dfs.datanode.CacheReportsNumOps",
        "seriesStartTime": 1542037501137,
        "supportsAggregation": true,
        "type": "COUNTER"
    },
    {
        "isWhitelisted": true,
        "metricname": "FSDatasetState.org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetImpl.EstimatedCapacityLostTotal",
        "seriesStartTime": 1542037495521,
        "supportsAggregation": true,
        "type": "GAUGE"
    },
],
    "toto": [
....

And what I need is to extract this : datanode, toto, etc. Only the name.

Can you help me please ?

I tried using jq without success.

tonio94
  • 460
  • 1
  • 6
  • 18

1 Answers1

2

You can use jq's keys functionality

jq 'keys' file.json

In the future try to improve on which words you use to describe the different parts the json data. You asked about objects in the text, but actually refer to the keys.

A more fitting title for the question would have been: "How to get all top level keys of json data using jq?" And with this, more correct, wording you find already answered questions like this one: How to get key names from JSON using jq

Also provide a complete and valid example structure and the expected result like this:

{
  "one_key": {
    "foo": "bar"
  },
  "another_one": {
    "bla": "bla"
  }
}

And desired result:

[
  "another_one",
  "one_key"
]