Unable to parse JSON "name" using tExtractJSON

Highlighted
Six Stars

Unable to parse JSON "name" using tExtractJSON

Hi all, 

 

I'm trying to parse the JSON pairs "name" from the below string but none of the combinations I used in tExtractJSON worked (I used both JsonPath and Xpath and nothing works). 

 

{"server_time":1535722727,"pairs":{"eur_usd":{"decimal_places":8,"min_price":0.00000001,"max_price":2.0,"min_amount":0.00000001,"max_amount":1000000.0,"min_total":0.0001,"hidden":0,"fee":0.25},"chf_usd":{"decimal_places":8,"min_price":0.00000001,"max_price":2.0,"min_amount":0.001,"max_amount":1000000000.0,"min_total":0.0001,"hidden":1,"fee":0.25},"jpy_usd":{"decimal_places":8,"min_price":0.00000001,"max_price":2.0,"min_amount":0.001,"max_amount":1000000000.0,"min_total":0.0001,"hidden":1,"fee":0.25}

 

I'm trying to get just the pairs names: "eur_usd", "chf_usd", "jpy_usd". Any help would be greatly appreciated. 


Accepted Solutions
Forteen Stars

Re: Unable to parse JSON "name" using tExtractJSON

something like:

 

Screen Shot 2018-09-18 at 02.23.50.png

 

 

use tLibrayLoad for - javax.json-api-1.1.2.jar

 

1st tJavaFlex

import java.util.*;
import javax.json.*;

JSONObject object = new JSONObject(row4.line);
String[] keys = JSONObject.getNames(object);

for (String key : keys)
{
    Object value = object.get(key);
}

globalMap.put("array_size", keys.length);

List keylist = Arrays.asList(keys);
globalMap.put("vk",keylist);

 

2nd tJavaFlex:

List<String> alist = (List<String>)globalMap.get("vk");

Integer ii = ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION"))-1;

globalMap.put("jkey",alist.get(ii));

Screen Shot 2018-09-18 at 02.29.09.png

 

it iterate over pair and put each key name into global variable for use on feature steps

Screen Shot 2018-09-18 at 02.30.36.png

 

-----------

View solution in original post


All Replies
Forteen Stars

Re: Unable to parse JSON "name" using tExtractJSON

-- edited

 

because You need Keys names, it not possible todo with standard Talend components

but possible with tJava

 

You can search on forum, but I can try to  find example from past project

-----------
Forteen Stars

Re: Unable to parse JSON "name" using tExtractJSON

something like:

 

Screen Shot 2018-09-18 at 02.23.50.png

 

 

use tLibrayLoad for - javax.json-api-1.1.2.jar

 

1st tJavaFlex

import java.util.*;
import javax.json.*;

JSONObject object = new JSONObject(row4.line);
String[] keys = JSONObject.getNames(object);

for (String key : keys)
{
    Object value = object.get(key);
}

globalMap.put("array_size", keys.length);

List keylist = Arrays.asList(keys);
globalMap.put("vk",keylist);

 

2nd tJavaFlex:

List<String> alist = (List<String>)globalMap.get("vk");

Integer ii = ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION"))-1;

globalMap.put("jkey",alist.get(ii));

Screen Shot 2018-09-18 at 02.29.09.png

 

it iterate over pair and put each key name into global variable for use on feature steps

Screen Shot 2018-09-18 at 02.30.36.png

 

-----------

View solution in original post

Six Stars

Re: Unable to parse JSON "name" using tExtractJSON

Thank you for this; this is great!

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog