0

I have this:

{
Item: [
        {
         key1: 'one',
         key2: 'two',
         key3: ["Sam", "Jack"]
        },
        {
         key1: 'one',
         key2: 'two',
         key3: ["Annie", "Jason"]
        }
     ]
}

So I wanted to get the object where its key3 contains "Annie". The best expression I come up with Items[].key3[?contain(@, 'Annie') == 'true'] which does not work.

2 Answers 2

1

Well it looks to me like you are having multiple typos and then one big issue in you JMESPath expression.

First things first, for your JSON to be totally valid:

  1. You should use double quotes, not single ones
  2. Your names should be inclosed in double quotes too

Source: https://restfulapi.net/json-syntax/

So, here is your correct JSON data:

{
  "Item": [
    {
      "key1": "one",
      "key2": "two",
      "key3": [
        "Sam",
        "Jack"
      ]
    },
    {
      "key1": "one",
      "key2": "two",
      "key3": [
        "Annie",
        "Jason"
      ]
    }
  ]
}

Then, in your JMESPath you have two typos:

  1. Your top level name is Item not Items
  2. The correct JMESPath expression is contains and not contain

And finally, but maybe the most important issue your contains expression is incorrect, it tests the return of contains against the string 'true' when contains does return a boolean true or false.

Because of this, you have to compare the return of contains against the raw string literal `true`.

So your correct query ends up being:

Item[].key3[?contains(@, `Annie`) == `true`]

Which gives:

[
  [],
  [
    "Annie"
  ]
]
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing commas (,) after key1 and key2.

Using javascript:

jsonObject= {
  Item: [
    {
      key1: 'one',
      key2: 'two',
      key3: ["Sam", "Jack"]
    },
    {
      key1: 'one',
      key2: 'two',
      key3: ["Annie", "Jason"]
    }
  ]
};

console.log(
  jsonObject.Item.filter(item=>item.key3.includes("Annie"))
);

2 Comments

Sorry, that's a typo. Thank you very much but could you provide an expression for JMESPath?
You are welcome! Sorry, I cannot help you with JMESPath - I'm not familiar with it (yet...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.