1

I am trying to extract and transform elements from a JSON document using JMESPath. Here is my test JSON array:

const search = jmespath.search;
const testData =
{
"ServiceAccount": [
    {
        "Type": "WIDGET",
        "ID": [
            {
                "OrderNum": "12345",
                "OrderTyp": "ABDCD"
            }
        ]
      }
    ]
};

I am trying to extract the value of the OrderNum key using the following JMESPath expression, but it returns null. Here is my search expression:

const result = search(testData, 'ServiceAccount.ID.OrderNum');
console.log(result);

Why is this not working?

1 Answer 1

2
const testData =
{
"ServiceAccount": [
    {
        "Type": "WIDGET",
        "ID": [
            {
                "OrderNum": "12345",
                "OrderTyp": "ABDCD"
            }
        ]
      }
    ]
};

const result = jmespath.search(testData, 'ServiceAccount[].ID[].OrderNum');
console.log(result);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @josip. Yes, your suggestion worked. I did not appreciate the significance of the square brackets. My sample testData represents only a small portion of a much larger JSON object that contains lots of square brackets.
Hi @josip - Do you know if it is a problem to use square brackets with JMESPath? Your solution involved removing the square brackets, but this renders my JSON object invalid.
@DanielAttard no it is not problem, i should not modify your example data, i took too little time to inspect the problem, my bad, i fixed my answer, now it works with your data structure. Regards.

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.