How-to - Filter json output - One value

Greetings,

Question

How-to - Filter json output and get one value.

What would be the best and quick way to accomplish that.

For example, if I have the following json output

[
{
"data": "{"public_ip":"172.83.47.196","region":"Ontario","country":"Canada","city":"Toronto","location":"43.7001,-79.4163","organization":"AS46562 Performive LLC","postal_code":"M5A","timezone":"America/Toronto"} "
}
]

and I want to get the public_ip output only
172.83.47.196

Example:

Example that is working with javascript when running in browser

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Convert JSON String to JavaScript Object and Access Individual Values</title>
</head>
<body>
    <script>
    // Store JSON data in a JS variable
    var json ='{"public_ip":"172.83.47.196","region":"Ontario","country":"Canada","city":"Toronto","location":"43.7001,-79.4163","organization":"AS46562 Performive LLC","postal_code":"M5A","timezone":"America/Toronto"}';
    
    // Converting JSON-encoded string to JS object
    var obj = JSON.parse(json);
    
    // Accessing individual value from JS object
    document.write(obj.public_ip + "<br>"); // Prints: 172.83.47.196
    document.write(obj.region + "<br>"); // Prints: Ontario
    document.write(obj.country); // Prints: Canada
    </script>
</body>
</html>

Hi @ansred, you can use a logic similar to your browser logic in n8n. n8n expressions contain JavaScript, so you could parse your JSON-like string and read the public_ip property like so:

So pretty much what you already have just wrapped inside JSON.parse() with an added .public_ip:

image

Hope this helps!

1 Like

That helped indeed. Quick and straightforward!
Thank you @MutedJam !!!

1 Like