GSON | Extract JSON's Root Name | JsonPath Or JsonPointer - Stack Overflow
JavaScript Object Notation (JSON) Pointer (2013)
syntax - json query and json path - Stack Overflow
Wouldn't JSONPatch, JSONPointer be better in Foundation? - Swift-DocC - Swift Forums
You can do it easily with Hibernate Validator 6.1.5.
You need to provide your own implementation of PropertyNodeNameProvider.
By implementing it, we can define how the name of a property will be resolved during validation. In our case, we want to read the value from the Jackson configuration.
Creating a validator:
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
.configure()
.propertyNodeNameProvider(new JacksonPropertyNodeNameProvider())
.buildValidatorFactory();
JacksonPropertyNodeNameProvider:
public class JacksonPropertyNodeNameProvider implements PropertyNodeNameProvider {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public String getName(Property property) {
if ( property instanceof JavaBeanProperty ) {
return getJavaBeanPropertyName( (JavaBeanProperty) property );
}
return getDefaultName( property );
}
private String getJavaBeanPropertyName(JavaBeanProperty property) {
JavaType type = objectMapper.constructType( property.getDeclaringClass() );
BeanDescription desc = objectMapper.getSerializationConfig().introspect( type );
return desc.findProperties()
.stream()
.filter( prop -> prop.getInternalName().equals( property.getName() ) )
.map( BeanPropertyDefinition::getName )
.findFirst()
.orElse( property.getName() );
}
private String getDefaultName(Property property) {
return property.getName();
}
}
More details You can find in documentation:
As far as I understood your question You are seeking path to your field. As there is no I/P JSON I have taken one example for you.
package jsonpath;
import java.util.List;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import static com.jayway.jsonpath.JsonPath.*;
public class GetPaths {
public static void main(String [] args) {
String json = "{\"top_field\": { \"mid_field\": [ { \"my_field\": true, }, { \"my_field\": true, } ], \"another_mid_field\": [ { \"my_field\": false } ] }}";
Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> pathList = using(conf).parse(json).read("$..my_field");
for(String path : pathList) {
System.out.println(path);
}
}
}
Will output exactly
$['top_field']['mid_field'][0]['my_field']
$['top_field']['mid_field'][1]['my_field']
$['top_field']['another_mid_field'][0]['my_field']
If you do some simple string replace on that one I think it´s a nice and easy solution. I`m not sure if you can get anything similar with plain Jackson/FasterXML. JsonPath uses Jackson under the hood.
You can get to more about Jayway JsonPath on official git repo