How to get the key name in data output?

#1

I get the data like this from WordPress API:
{
“tags”: {
“School”: {},
“Work”: {},
“Others”: {}
},
“type”: “post”,
“status”: “publish”,
“password”: “”
}

How can I get the name of tags (School, Work, Others)?

#2

Hi @yappa,

you should use a Code action and define an input tags and assign {{your-wp-webhook-name.data__tags}}.
Inside Code you can write your javascript code to parse your inputs.tags

#3

Hi CaosMkt: Can you help me to write the code please?

(Daniel Beckett) #4

@yappa

The variable name isn’t included as an output unfortunately. If you know what data you are expecting then you could use the Text service to replace text from the returned data so that it changes it to a desired value.

#5

Hi @yappa,

create a Code action named Get Tags with this code:

var tags = {{your-wp-webhook-action-name.data__tags}};
var tagName;
var tagsNames = [];
    
for (tagName in tags) {
  tagsNames.push(tagName);
}

resolve(tagsNames);

Replace your-wp-webhook-action-name with your webhook action name, now you can access the array of your tags with {{get_tags.result}}

2 Likes
Use field NAME (not just value) from previous step in later step?
#6

Hi @CaosMkt

Thanks for this but it didn’t work for me. All I got was a list of numbers from 0 to 171. My “tags” are integers (ids) - maybe that’s why? My tag input looks like this:

{“tags”:
{“14676”:“ABC”,
“14677”:“DEF”,
“14678”:“XYZ”}
}

Any help would be really appreciated it! Thanks!

#7

Hi @candle, the code returns an array with your keys, it doesn’t matter if numeric or string
I tried it and output result was:

[
  "14676",
  "14677",
  "14678"
]

What do you try to do?

#8

Sorry I should have replied, as I figured it out.
I created an input and used that instead of putting the equivalent to this {{your-wp-webhook-action-name.data__tags} directly in the code.

eg., I created an input called “hellotags” and put the value as the equivalent to {{your-wp-webhook-action-name.data__tags}

so my code was:

var tags = inputs.hellotags;
var tagName;
var tagsNames = [];
    
for (tagName in tags) {
  tagsNames.push(tagName);
}

resolve(tagsNames);

The problem was that for some reason flowxo escaped the quotes in the input, so the input looked like this:

{\"tags\":
{\"14676\":\"ABC\",
\"14677\":\"DEF\",
\"14678\":\"XYZ\"}
}

I don’t know why FlowXo did that, but anyway, if I just did what you posted by inserting the value directly into the code without using inputs, it worked just fine

#9

The problem is that inputs are casting to string, in my example i use {{}} notation directly in code so the string in attributes is not escaped and interpreted as a json string.

#10

I found a simpler code so just want to share:

var tags = {{make_a_http_request.data__tags}};
var tagNames = Object.keys(tags)

resolve(tagNames);