How to parse a json that has been extracted from openstack with a tRestAPI

Four Stars

How to parse a json that has been extracted from openstack with a tRestAPI

Hello,
I have a tRestAPI component that sends a request to have metrics present on openstack. I connect this tRestAPI to a tFileOutputRow, I get a json. Now I would like to parry this json to have only a few metrics (for example the name of the vm presented on openstack and their CPU). I tried with a tMap but I do not have the arboressance of my json. Now, when I open my file, I have all the data. but I can not get them in my tMap to parse.

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

You will need to extract the key fields using a tExtractJSONFields component. This is explained here: https://help.talend.com/reader/g8zdjVE7fWNUh3u4ztO6Dw/Je6AV_TMnYHC_MpEtaMBaA. 

Four Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

I did a word search (ctrl+f) on the link you gave me ... There is no mention of the word "json". Are you sure it's the right link?

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Sorry about that. Not sure how that happened. The link I meant to send was this one....https://help.talend.com/reader/g8zdjVE7fWNUh3u4ztO6Dw/Je6AV_TMnYHC_MpEtaMBaA

Four Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Thank you for your link, I tried but I did not find a simple way to parse my json file. I have several informations (metrics of VM openstack) collected in a JSON file and I would like to simply take some information (for example the name of the vm or the CPU etc ...). The biggest problem is that in my tRestAPI component I only have two variables (Body and ERROR_CODE) so I do not have a tree and the tFileoutputJson can not get into the json get by the tRestAPI.

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

You are misunderstanding how JSON works. It is not a Document (or a tree object). It is a String that is structured in a tree. It doesn't matter if that tree looks like this....

{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

....or looks like this.....

{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}

....it is exactly the same and will be parsed by a proper JSON parser. 

 

The tExtractJSONFields component uses a proper parser, you just need to use the correct JSONPath syntax. You can learn about this online. Try here: https://www.pluralsight.com/blog/tutorials/introduction-to-jsonpath

 

I'm afraid interacting isn't easy when you start. There just isn't a way that you can get away with not struggling for a bit. You can maybe get your REST service to return XML, that will give you a tree object.

Four Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Excellent example!

let's take your example ==> {"glossary": {"title": "glossary example", "GlossDiv": {"title": "S", "GlossList": {"GlossEntry": {"ID": " SGML "," SortAs ":" SGML "," GlossTerm ":" Standardized generalized markup language "," Acronym ":" SGML "," Abbrev ":" ISO 8879: 1986 "," GlossDef ": {" para " : "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"]}, "GlossSee": "markup"}}}}}}

If I want to recover in a csv file the variable "title": "example glossary", how to do? it's all I ask, it's been several weeks that I try but I do not find the solution that's why I ask you.

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

If the JSONPath is what you want, it would be ....

$.glossary.title

Have a look here http://jsonpath.com/

You can test working with JSONPath quite easily with your documents.

Four Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Ah thank you ! That's exactly what I wanted ! the website http://jsonpath.com/ is perfect. Now my question is how to do the same thing on Talend! I tried to set up a simple Job, a tFileInputJSON that will read a JSON file that I have locally in my pc, connected to a tLogRow. I would just like to extract a piece of data from the batch of data present in the JSON. Help me please I feel that you have the solution to my problem!

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Follow this tutorial: https://help.talend.com/reader/9q55KsfASqX0qY4GVhEDNQ/_FLAj764Hj9UMDUhCE5rew

 

Use the online tester to work out your paths and then apply them as shown above. The only thing you will need to take into account is the Looping parameter. Pay attention to how that is explained.

Sixteen Stars

Re: How to parse a json that has been extracted from openstack with a tRestAPI

Did this resolve your issue? If can you accept the solution please?