com.jayway.jsonpath.JsonPathException Java Examples

The following examples show how to use com.jayway.jsonpath.JsonPathException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonPathParser.java    From aws-lambda-jenkins-plugin with MIT License 6 votes vote down vote up
public Map<String, String> parse(String payload){
    Map<String, String> result = new HashMap<String, String>();
    if(StringUtils.isNotEmpty(payload)) {
        DocumentContext documentContext = JsonPath.parse(payload);

        for (JsonParameter jsonParameter : jsonParameters) {
            if (StringUtils.isEmpty(jsonParameter.getJsonPath())) {
                result.put(jsonParameter.getEnvVarName(), payload);
            } else {
                try {
                    result.put(jsonParameter.getEnvVarName(), documentContext.read(jsonParameter.getJsonPath()).toString());
                } catch (JsonPathException jpe) {
                    logger.log("Error while parsing path %s for environment variable %s.%nStacktrace:%n%s%n",
                            jsonParameter.getJsonPath(), jsonParameter.getEnvVarName(), LogUtils.getStackTrace(jpe));
                }
            }
        }
    }

    return result;
}
 
Example #2
Source File: JSONIOTest.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
@Test
public void countMatches () throws ManipulationException
{
    String pluginsPath = "$..plugins";
    String reposURLPath = "$.repository.url";
    try
    {
        DocumentContext doc = jsonIO.parseJSON( pluginFile );

        List o = doc.read ( pluginsPath );
        assertEquals( 1, o.size() );

        o = doc.read ( reposURLPath );
        assertEquals( 1, o.size() );
    }
    catch (JsonPathException e)
    {
        throw new ManipulationException( "Caught JsonPath", e );
    }
}
 
Example #3
Source File: JsonRecordParser.java    From kafka-connect-zeebe with Apache License 2.0 5 votes vote down vote up
private void applyVariableWithFallback(
    final Message.Builder builder, final DocumentContext document) {
  try {
    builder.withVariables(document.read(variablesPath));
  } catch (final JsonPathException e) {
    LOGGER.trace("No variables found, fallback to the whole document as payload");
    builder.withVariables(document.json());
  }
}
 
Example #4
Source File: JsonRecordParser.java    From kafka-connect-zeebe with Apache License 2.0 5 votes vote down vote up
private void applyTimeToLiveIfPossible(
    final Message.Builder builder, final DocumentContext document) {
  try {
    builder.withTimeToLive(document.read(ttlPath));
  } catch (final JsonPathException e) {
    LOGGER.trace("No timeToLive found, ignoring");
  }
}
 
Example #5
Source File: JsonPathAssert.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
public JsonPathAssert hasNoPath(String jsonPath) {
	try {
		Object value = this.actual.read(jsonPath);
		failWithMessage("The path '" + jsonPath + "' was not expected but evaluated to " + value);
		return null;
	}
	catch (JsonPathException ex) {
		return this;
	}
}
 
Example #6
Source File: JsonPathAssert.java    From spring-cloud-open-service-broker with Apache License 2.0 5 votes vote down vote up
public JsonPathAssert hasNoPath(String jsonPath) {
	try {
		Object value = actual.read(jsonPath);
		failWithMessage("The path " + jsonPath + " was not expected but evaluated to " + value);
		return null;
	}
	catch (JsonPathException e) {
		return this;
	}
}
 
Example #7
Source File: KeyPairPlugin.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<String> getKeyName(final EC2InstanceContext context) {
    try {
        return Optional.ofNullable(trimToNull(JsonPath.read(context.getInstanceJson(), "$.keyName")));
    } catch (final JsonPathException ignored) {
        return Optional.empty();
    }
}
 
Example #8
Source File: LambdaEventUtil.java    From fullstop with Apache License 2.0 5 votes vote down vote up
static Optional<String> getFromJSON(final String json, final String... jsonPaths) {
    Optional<String> result = empty();

    for (final String jsonPath : jsonPaths) {
        try {
            result = Optional.ofNullable(JsonPath.read(json, jsonPath));
            if (result.isPresent()) {
                return result;
            }
        } catch (final JsonPathException ignored) {
            // DO NOTHING
        }
    }
    return result;
}
 
Example #9
Source File: AmiIdProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<String> readAmiIdFromJson(final EC2InstanceContext context) {
    try {
        return Optional.ofNullable(JsonPath.read(context.getInstanceJson(), IMAGE_ID_JSON_PATH));
    } catch (final JsonPathException ignored) {
        return empty();
    }
}
 
Example #10
Source File: JSONIOTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
@Test (expected = ManipulationException.class)
public void updateWithInvalidPath () throws ManipulationException
{
    String modifyPath = "$.I.really.do.not.exist.repository.url";
    try
    {
        DocumentContext doc = jsonIO.parseJSON( pluginFile );
        doc.set( modifyPath, "https://maven.repository.redhat.com/ga/" );
    }
    catch (JsonPathException e)
    {
        throw new ManipulationException( "Caught JsonPath", e );
    }
}
 
Example #11
Source File: JSONManipulator.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
void internalApplyChanges( Project project, JSONState.JSONOperation operation ) throws ManipulationException
{
    File target = new File( project.getPom().getParentFile(), operation.getFile() );

    logger.info( "Attempting to start JSON update to file {} with xpath {} and replacement '{}' ",
                 target, operation.getXPath(), operation.getUpdate() );

    DocumentContext dc = null;
    try
    {
        if ( !target.exists() )
        {
            logger.error( "Unable to locate JSON file {} ", target );
            throw new ManipulationException( "Unable to locate JSON file {}", target );
        }

        dc = jsonIO.parseJSON( target );

        List<?> o = dc.read( operation.getXPath() );
        if ( o.size() == 0 )
        {
            if ( project.isIncrementalPME() )
            {
                logger.warn ("Did not locate JSON using XPath " + operation.getXPath() );
                return;
            }
            else
            {
                logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() );
                throw new ManipulationException( "XPath did not resolve to a valid value" );
            }
        }

        if ( isEmpty( operation.getUpdate() ) )
        {
            // Delete
            logger.info( "Deleting {} on {}", operation.getXPath(), dc );
            dc.delete( operation.getXPath() );
        }
        else
        {
            // Update
            logger.info( "Updating {} on {}", operation.getXPath(), dc );
            dc.set( operation.getXPath(), operation.getUpdate() );
        }

        jsonIO.writeJSON( target, dc );
    }
    catch ( JsonPathException e )
    {
        logger.error( "Caught JSON exception processing file {}, document context {} ", target, dc, e );
        throw new ManipulationException( "Caught JsonPath", e );
    }
}
 
Example #12
Source File: ConfigIO.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
public Properties parse ( final File workingDir) throws ManipulationException
{
    Properties result = new Properties();
    File propertyFile = new File( workingDir, propertyFileString );
    File yamlPMEFile = new File( workingDir, yamlPMEFileString );
    File yamlPNCFile = new File( workingDir, yamlPNCFileString );
    File jsonPNCFile = new File( workingDir, jsonPNCFileString );

    if ( propertyFile.exists() && ( yamlPMEFile.exists() || yamlPNCFile.exists() || jsonPNCFile.exists() ) )
    {
        throw new ManipulationException( "Cannot have both yaml, json and property configuration files." );
    }
    else if ( yamlPMEFile.exists() && yamlPNCFile.exists() )
    {
        throw new ManipulationException( "Cannot have both yaml configuration file formats." );
    }
    else if ( ( yamlPMEFile.exists() || yamlPNCFile.exists() ) && jsonPNCFile.exists() )
    {
        throw new ManipulationException( "Cannot have yaml and json configuration file formats." );
    }
    if ( yamlPMEFile.exists() )
    {
        result = loadYamlFile( yamlPMEFile );
        logger.debug( "Read yaml file containing {}.", result );
    }
    else if ( yamlPNCFile.exists() )
    {
        result = loadYamlFile( yamlPNCFile );
        logger.debug( "Read yaml file containing {}.", result );
    }
    else if ( jsonPNCFile.exists() )
    {
        try
        {
            result.putAll( JsonPath.parse( jsonPNCFile ).read ( "$.pme", Map.class ) );
        }
        catch ( IOException | JsonPathException e)
        {
            throw new ManipulationException( "Caught exception processing JSON file.", e );
        }
        logger.debug( "Read json file containing {}.", result );
    }
    else if ( propertyFile.exists() )
    {
        result = loadPropertiesFile( propertyFile );
        logger.debug( "Read properties file containing {}.", result );
    }
    return result;
}