U
U
uristsoapmaker2020-05-22 20:11:34
JSON
uristsoapmaker, 2020-05-22 20:11:34

JsonPath - how to pull values ​​from Json, except for certain ones?

Hello.
There is this Json:
[
{
"accID": "3asdasd321asdasdfsadf2",
"test": "one",
"isGood": "true",
},
{
"accID": "Not Found",
"test": "two",
"isGood": "true",
},
{
"accID": "1asdasd121asdasdfsadf5",
"test": "five",
"isGood": "false",
}
]


I want to get all accID values ​​but without 'Not Found' , like this:
[
"3asdasd321asdasdfsadf2",
"


$.[?(@.accID != 'Not Found')] - this one returns the same Json, but without sections containing 'Not Found'
$.accID[?(@.accID != 'Not Found' )] - this does not work.
How can I set up a filter to get the desired result without 'Not Found'?

Help, please, I'm already tormented.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2020-05-22
@uristsoapmaker

jsonContext = JsonPath.parse(jsonString);

    List<String> data = jsonContext.read("$.[?(@.acc != 'Not Found')].acc");
    assertThat(data, notNullValue());
    assertThat(data.size(), greaterThan(0));
    System.err.println("Results: " + data);

gives
Results: ["3asdasd321asdasdfsadf2","1asdasd121asdasdfsadf5"]

S
Sergey Ilyin, 2020-05-22
@sunsexsurf

1/ dict inside list right?
try breaking it down into steps:

a= your list
# так как это список, нам нужно посмотреть все элементы в списке
# в вашем случае, элементы - это словарь (ну или json - не важно)
for i in a: 
    if i['accID'] != 'Not Found':
        print(i['accID'])

# в одну строку
print(list(i['accID'] for i in a if i['accID'] != 'Not Found'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question