JAVA get keys from JSON object GREP command

Good afternoon Wapplers,

I have a specific question regarding the ability to display an API call response after the call has been submitted.

Currently, there are response fields that have keys that needed to be identified and then placed within the dynamic data of the text field.

Currently if I have a single response that does not look for an Identifier, my dynamic data field reads as follows;
<p class="cal7a text-lg-center">{{AnnualCarbonTonnes.value}}</p>

However if I have a data field that has a response that is associated with a Key Identifier, I am a little at a loss as to how to inject that data. I would assume that I would need to grab the Keys from the JSON Blob, and potentially use a GREP command to associate the specific data.

Below is an example of the ID: "Veh0Toyota2018CityMPGAdjustment":
The key is 0Toyota2018

Hope I am explaining this correctly.

Sorry to tag @Apple @kfawcett @patrick

Something like this you want?

{{ $value["Veh" + key + "CityMPGAdjustment"] }}

Does this make sense?

{{#each Veh}}
  <p class="cal7a text-lg-center">{{ this.VehManufacturer }} {{ this.VehYear }} {{ this.VehName }}</p>
{{/each}}

or do I define the key as VehYear and VehManufacturer and use what you sent?

That kind of makes sense, but I’m very confused

Would you be doing something like this?

{
    "0Toyota2018": {
        "VehCityMPGAdjustment": 123
    }
}

That would be the ideal structure of data I guess. In Wappler terms this is a “keyed array” (e.g.: 0Toyota2018)

What i am trying to do is receive the info from the API call with the associated keys.

Here is the response back from the swagger docs.

"Veh0Toyota2018VehName": "We will designate the name of this vehicle 0Toyota2018",

the 0 is the first Vehicle repeat form submitted the Toyota is associated with the ID input VehManufacturer, and 2018 is associated with the ID input VehYear

You wrote this:

{{#each Veh}}
  <p class="cal7a text-lg-center">{{ this.VehManufacturer }} {{ this.VehYear }} {{ this.VehName }}</p>
{{/each}}

So you want this API response (array):

[
  {
    "VehName": "Toyota",
    "VehManufacturer": "Toyota",
    "VehType": "Hybrid",
    "VehYear": 2012
  }
]

Or (keyed array)

{
    "0Toyota2018": { 
        "VehName": "Toyota",
        "VehManufacturer": "Toyota",
        "VehType": "Hybrid",
        "VehYear": 2012
    }
}

There’s no way around it, either you ask your back-end team to change or you’ll have to convert to this ideal structure by yourself. I did a similar job for kfawcett before

So, I’m going to ask, do you want to convert to this ideal structure by yourself? You’d probably have to write a custom JavaScript script/formatter to uncrap that data

Edit: I let ChatGPT explain my frustation:

The phrase “you have to write a script to uncrap that data” means that the data in question is of poor quality or badly formatted and requires significant effort to clean or transform it into a more usable format. In order to perform this task, the speaker suggests that the best approach would be to create a script, which is a program that can automate the process of cleaning up and organizing the data. The use of the word “uncrap” is informal and conveys a sense of frustration or dissatisfaction with the current state of the data.

LOL… hilarious. Yes it is an issue of backend verse frontend translation. I think I agree with you that I will need to speak with the backend group and work out a simplified work around. What it looks like is that they are sending the response back and adding the Key data to the response field so that it will be difficult for me to capture the response.

Is there a way that you know of to inject that Key JASON data in the response? Meaning,

{{ Veh[0].value }} + {{ VehManufacturer.value. }} + {{ VehYear.value }} + {{ VehName }}

The above is just a guess.

Who did the initial specification for what you send? Was it you?

Yes, you have to find the syntax to access the root object. Sometimes it’s like this:

{{ $value["Veh" + key + "VehManufacturer"] }}

Where $value is the root object, but I think this is only for repeaters (which won’t work in your case).

Alternatively, you can access the root object by using the full path like sc_response.data["Veh" + key + "VehManufacturer"], which you’ll have to figure by looking at the browser console (dmx.app.data) and traversing the variables till you find what you want

This is very helpful. I will do some experimenting.

I did but my mathematician partner created the python system.

So if the call looks like this

"Vehicles": [{"VehManufacturer": "Toyota", "VehYear": "2018", "VehMilesPerGallon": 32.5, "VehMilesDrivenPerYear": 18000.0 , "VehPctCityDriving": 80.0},
               {"VehManufacturer": "Ford", "VehYear": "2020", "VehMilesPerGallon": 30.0, "VehMilesDrivenPerYear": 22000.0 , "VehPctCityDriving": 20.0}],

then it would be
{{ Vehicle.value[“Veh” + “Vehmanufacuture” + “VehYear” + “GalPerYear”] }}. or do I use the sc_response.data but do I still have to tell it Vehicle.value?

Such expression would evaluate to Vehicle.value.VehVehmanufacutureVehYearGalPerYear, which is clearly not what you want :stuck_out_tongue:

You could use a repeater and then it becomes easy to output (I think):

{{ VehManufacturer }}

Because you’d be iterating the API response (.Vehicles)

Or you access by array index: Vehicles[0].VehYear

Do you have any documentation on how to use a repeater?

man I am so tired! you and I are both crazy… LOL

Last question for you I swear (at least for this evening :rofl:).

How do I show repeat form data that is more than just one?

Meaning right now I have the dynamic data showcase the following,

<p class="cal7a text-lg-center">{{AirTravel.items[0].AirHrsOnCommercialFlights.value}}</p>

As you know the [0] represents the first segment of the repeat form input. How do I expand the code to show mulitple inputs of the same ID? meaning the entire list?

I’m struggling really hard to understand you, because you’re not using the correct technical terms

What’s an input?

Same index? E.g. in this case: [0]

The entire list of items? (AirTravel.items)

And now I’m going to sleep, good bye

P.S.: Please find a tutorial for the repeater, I’m not a front-end person

sorry to bug you buddy. Sleep well

1 Like