0

So, lets say I had a JSON File like this:

{
  "content": [
    {
      "word": "cat",
      "adjectives": [
        {
          "type": "textile",
          "adjective": "fluffy"
        },
        {
          "type": "visual",
          "adjective": "small"
        }
      ]
    },
    {
      "word": "dog",
      "adjectives": [
        {
          "type": "textile",
          "adjective": "fluffy"
        },
        {
          "type": "visual",
          "adjective": "big"
        }
      ]
    },
    {
      "word": "chocolate",
      "adjectives": [
        {
          "type": "visual",
          "adjective": "small"
        },
        {
          "type": "gustatory",
          "adjective": "sweet"
        }
      ]
    }
  ]
}

Now, say I wanted to search for two words. For example, "Fluffy" and "Small." The problem with this is that both words' adjectives contain small, and so I would have to manually search for which one contains fluffy. So, how would I do this in a quicker manner?

In other words, how would I find the word(s) with both "fluffy" and "small"

EDIT: Sorry, new asker. Anything that words in a terminal is fair game. jq is a really great JSON searcher, and so this is preferred, and sorry for the confusion. I also fixed the JSON

  • Could use the Array.filter method? See [this answer](https://stackoverflow.com/questions/18306675/getting-specific-value-from-json-using-javascript). – aOSiOSDev Oct 23 '22 at 23:10
  • @aOSiOSDev I can see how this would help, but I don t know how I would implement it here... Could I see an example? – The Gaming Weirdo Oct 24 '22 at 01:01
  • @TheGamingWeirdo where do you want to do this? Which programming language? Do you write a program which handles this JSON or is it a file on your file system? – knittl Oct 24 '22 at 07:38

2 Answers2

0

A command-line solution would be to use jq:

jq -r '.content[] | select(.adjectives[].adjective == "fluffy") | .word' /pathToJsonFile.json

Output:

cat

Are you looking for something like this? Do you need a solution that uses other programming languages?

(P.S. your JSON example appears to be invalid)

Seasers
  • 466
  • 2
  • 7
  • 1
    After fiddling with this, I figured it out. Basically, to search for multiple, you just use `and` So, like this: `jq -r '.content[] | select(.adjectives[].adjective == "fluffy") and select(.adjectives[].adjective == "small") | .word' /pathToJsonFile.json ` – The Gaming Weirdo Oct 24 '22 at 19:34
0

Since is now fair game (this was only clarified later in the comments), here is one solution using jq.

First, fix the JSON to be actually valid:

{
  "content": [
    {
      "word": "cat",
      "adjectives": [
        {
          "type": "textile",
          "adjective": "fluffy"
        },
        {
          "type": "visual",
          "adjective": "small"
        }
      ]
    },
    {
      "word": "dog",
      "adjectives": [
        {
          "type": "textile",
          "adjective": "fluffy"
        },
        {
          "type": "visual",
          "adjective": "big"
        }
      ]
    },
    {
      "word": "chocolate",
      "adjectives": [
        {
          "type": "visual",
          "adjective": "small"
        },
        {
          "type": "gustatory",
          "adjective": "sweet"
        }
      ]
    }
  ]
}

Then, the following jq filter returns an array containing the words which contain both adjectives:

.content
| map(
    select(
        .adjectives as $adj
        | all("small","fluffy"; IN($adj[].adjective))
    )
    | .word
)

If a non-array output is required, and only one word per line, use .[] instead of map (either after content or as a final filter), e.g.:

jq -r '.content[]
    | select(
        .adjectives as $adj
        | all("small","fluffy"; IN($adj[].adjective))
    )
    | .word'
knittl
  • 246,190
  • 53
  • 318
  • 364