How do I show only top level JSON entries that are not arrays or objects with jq version 1.3?

nfz11

I have a JSON structure and I only want to print the entries that are simple types, e.g. not arrays or objects.

Sample input:

{
  "person": {
    "address": {
      "city": "NY",
      "street": "Wall Street"
    },
    "god": true,
    "nicks": [
      "Lar",
      "L",
      "Yo"
    ],
    "name": "Larry",
    "id": 1
  }
}

Expected output:

"god": true,
"name": "Larry",
"id": 1

I tried many different variations on this which did not work:

jq '.person | with_entries(.) | select(map(type) != "array" and map(type) != "object")'

Please note that I am on jq version 1.3.

Thanks in advance for any help.

Jeff Mercado

You're not really doing anything with the with_entries call there. But you probably should be filtering there. Filter the entries where the value's type is neither an array nor an object.

.person | with_entries(select(.value | type | . != "array" and . != "object"))

If you were on a more recent version of jq, you could filter using the scalars builtin which effectively does the same thing.

.person | with_entries(select(.value | scalars))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I transform this JSON data using JQ to extract each nested array element to the top level in turn?

How do I add JSON array entries into a JSON object using jq?

JQ : Select arrays both top level and nested

How do I get jq to return unique results when json has multiple identical entries?

How do I get data from only the top few XML entries using AJAX

How do I change the Table of Contents in Word 2010 to show only headings 1-3?

How do I extract two arrays from a JSON with nested objects

How do you assign json objects in jq?

jq: error: canonical/0 is not defined at <top-level>, line 1. Not able to read \ in json using jq

How can I do top level await

How do I check out only top level files from git?

How do I show only 1 scaled image in fineuploader

How to merge different objects and arrays in the same json file using jq

How do I SELECT a column of json objects in Postgres so that the rows returned are json arrays of arrays?

How do I merge JSON objects into 1 JSON object

How do i limit the array to store only 3 objects in it?

How do i show links at the top of Gitbook?

How do I get all first objects of my json using jq?

How do I use JQ to select a handful of objects matching an ID in a large json file?

How do I support multiple top level namespaces in a single json schema document? This is for helm sub charts

How do I test if a Javascript object has only empty objects at the last nested level?

current directory only shown on top of terminal, how do I show it inside?

Show only top 3 entries and rest as "Others" in stacked barplot using ggplot2

How do I only show git info in the $PS1 if I am only in a git directory?

If there are no arrays for css, how do I organize many similar entries?

How do I parse JSON data which contains only arrays and nested arrays with no property names?

How do I do top 1 in Oracle?

How do I show variable values with zero entries in PROC SQL?

Convert Json table arrays to objects with jq