csv to json integer parse error using placeholders

93 Views Asked by At

so i have one csv file from there i'm parsing data in json, but there is integer conversion issue.

  • def foo = '10'

  • string json = { bar: '#(1 * foo)' }

  • match json == '{"bar":10.0}'

  • string json = { bar: '#(parseInt(foo))' }

  • match json == '{"bar":10.0}'

so i tried both ways to convert an string into integer but i am getting below error message: "JSON parse error: Cannot deserialize value of type java.lang.Integer from String "(1 * total_amount)": not a valid java.lang.Integer value". And 1000 is the value i'm giving in a csv file.

4.0.0

<groupId>org.karateauto</groupId>
<artifactId>Karate-Lead_Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>11</java.version>
    <maven.compiler.version>3.11.0</maven.compiler.version>
    <maven.surefire.version>3.0.0</maven.surefire.version>
    <karate.version>1.4.1</karate.version>
</properties>
<dependencies>
    <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-junit5</artifactId>
        <version>${karate.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-core</artifactId>
        <version>${karate.version}</version>
        <classifier>all</classifier>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <testResources>
        <testResource>
            <directory>src/test/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
            <configuration>
                <argLine>-Dfile.encoding=UTF-8</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>
Above is my pom file.

{ "action": "#(action)", "currency": "#(currency)", "cptrid": "#(cptrid)", "flexi1": "#(flexi1)", "flexi2": "#(flexi2)", "flexi3": "#(flexi3)", "flexi4": "#(flexi4)", "sys_id": "#(sys_id)", "lead_id": "#(lead_id)", "record_id": "#(record_id)", "mode_of_payment": "#(mode_of_payment)", "total_amount": "#(1 * total_amount)", "instrument_ref": "#(instrument_ref)", "instrument_date": "#(instrument_date)", "bank_name": "#(bank_name)", "branch_detls": "#(branch_detls)", "receipt_no": "#(receipt_no)" }

and above is the way i'm using placeholder for total amount.

2

There are 2 best solutions below

4
Peter Thomas On

I ran the following and it worked perfectly in Karate 1.4.1

* def foo = '10'
* string json = { bar: '#(1 * foo)' }
* match json == '{"bar":10}'
* string json = { bar: '#(parseInt(foo))' }
* match json == '{"bar":10}'

You have something else going on then, so follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

1
cyrilgeorge153 On
Feature: Integer parsing feature

  Scenario Outline: Integer parsing scenario
    Given url 'https://reqres.in/api/users'
    And request {"name":'#(name)',"job":'#(parseInt(job))'}
    When method post
    Then status 201

    Examples: 
      | read('classpath:testdata/create_post_testdata.csv').slice(0, 2) |

csv file:

name,job
Json-server1,1
Json-server2,2