javascript help to change a method structure

Viewed 70

I have the following code, It is a recursive function that I use it to parse complex Json responses, it detects if the current code is a String, JsonObject or Array. This function is very helpful for those who use Karate or they want to parse complex JSON response

function parseIt0(data, [key, ...path]) {
    return (Array.isArray(data) ? data : [data]).reduce((acc, obj) => {
        if (path.length) {
            acc.push(parseIt0(obj[key], path));
        } else if (obj[key]) {
            acc.push(obj[key]);
        }
        return acc;
    }, []).flat();
}

function getValue(response, s) {
    if (!response) return null;
    if (!s) return response;
    var path = s.split('.');
    return parseIt0(JSON.parse(response), path).flat();
}

//which can be used like this
var response2 = '{"data":[{"type":"node--base_product_coffee","id":"6dbb5a52-13ea-4f74-8af9-eb9e3ba45918","date":"1990","data1":[{"type1":"product_coffee1","id1":"6dbb5a52-13ea-4f74-8af9-eb9e3ba45777","date1":[{"res":"oui"},{"res":"non"}]},{"type1":"product_coffee2","id1":"6dbb5a52-13ea-4f74-8af9-eb9e3ba45666","date1":[{"res":"ouiii"},{"res":"nonnn"}]}]}]}';

console.log(getValue(response2, 'data.data1.date1.res');
//which gives : ['oui','non','ouiii','nonnn']

The problem with this code is I can not use it in Karate Framework because it gives me error on this part [key, ...path], it does not accept it.

So my question if anyone can help me to write the same function parseIt0 like this parseIt0(response, path) the path will be an array

I started by writing this but it I did not successed

removeItemOnce = function(arr, value) {
    var index = arr.indexOf(value);
    if (index > -1) {
        arr.splice(index, 1);
    }
    return arr;
}

function parseIt1(data, path) { 
    return (Array.isArray(data) ? data : [data]).reduce((acc, obj) => {
        if (path.length) {
            acc.push(parseIt1(obj[path[0]], removeItemOnce(path, path[0])));
        } else if (obj[path[k]]) {
            acc.push(obj[path[0]]);
        }
        return acc;
    }, []).flat();
}

function getValue2(response, s) {
    if (!response) return null;
    if (!s) return response;
    var path = s.split('.');
    return parseIt1(JSON.parse(response), path).flat();
}

// this in order to use it like the previous code
console.log(getValue2(response2, 'data.data1.date1.res');

Can anyone help me Thanks

1 Answers

I'm unable to read or understand your code, but let me just say that Karate 1.0 should make it easier to handle JSON and do these type checks.

Please read this part of the upgrade guide: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#type-conversion

  • new karate.fromString() to auto convert from JSON or XML strings within JS blocks
  • new karate.typeOf() to detect what "type" a given object is

And you will be able to use ES6 looping constructs:

* array.forEach(x => console.log(x))

Finally be aware of the Karate JSON helpers: https://github.com/intuit/karate#json-transforms

If you are still stuck, please simplify your question and ask one specific point at a time. But yes, there may be others with more patience than me.

Related