Extracting values from JMSE - javascript

I want extract text values from every sub objects the given JSON structure . For do that I have used following JMSEPath query translations.en.*[0].[*].children.text but I was not unable to extract the value . Can someone suggest me a correct query or other approches

It may be better if you add to the question the expected output as well.
Making a big assumption that you want all text values "flatten up" into a single array you can try this: translation.en.*[].children[].text
Ref https://jmespath.org/tutorial.html#flatten-projections

Related

How to retrieve the value of the first field in an object without its name?

I am using the REST Countries API to retrieve data from different countries, the point is to generate a HTML code to insert, depending on the country data.
But as I'm trying to point the language data in my HTML code, it of course returns me an object.
Here are two examples of the data from the API:
languages: {cat: 'Catalan'}
languages: {fra: 'French'}
The line of code in my HTML:
<p class="country__row"><span>🗣️</span>${data.languages}</p>
I would like to retrieve the values of the first field in those objects (Catalan or French) without having to, of course, write ${data.languages.fra} in my HTML code.
Thank you for your help.
I tried ${data.languages[0]} and ${data.languages[0].value}, doesn't work.
You can use the Object.keys() function to get the list of keys from an object.
There are other similar functions as well that you can use such as Object.values() or Object.entries().
As far as your use case is concerned try any one of the solutions mentioned below:
${data.languages[Object.keys(data.languages)[0]]}
// The `0` index can be replaced with i if using a loop.
${Object.values(data.languages)[0]}
You can write as Object.values(data.languages) which would give you values of object.

Way to determine XPath to retrieve data of a specific attribute

I am working on an integration and the response I am getting in XML format. I need to parse it and get the attribute values using javascript. I am trying to get value from node <ab:specific_field> with <ab:field_reference>id is commodity or cosmetic. I am struggling to go trough nodes and extract <ab:Value> node value which is the value I need to extract.
I tried with //ab:Main_Data/ab:specific_field[1]/ab:Value, but no luck. Can anyone help me to write a correct XPath to extract value from <ab:value> node.
<ab:Response_Data>
<ab:MainData>
<ab:reference>....</ab:refernce>
<ab:information....<ab..info>
<ab:specific_field>
<ab:Field_Reference>
<ab:ID type="WID">123</ab:ID>
<ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Commodity</ab:ID>
</ab:Field_Reference>
<ab:Value>Medicine</ab:Value>
</ab:specific_field>
<ab:specific_field>
<ab:Field_Reference>
<ab:type="WID">1234</ab:ID>
<ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Cosmetic</ab:ID>
</ab:Field_Reference>
<ab:Value>Powder</ab:Value>
</ab:specific_field>
</ab:MainData>
</ab:Response_Data>
Your XML has errors. If you have more than one ab:ID fields you can filter one of them with a property.
To get ab:ID nodes:
//ab:ID[#wd:parent_id="custom-API-Service"]
To get ab:ID text nodes:
//ab:ID[#wd:parent_id="custom-API-Service"]/text()
Try this
//ab:maindata/ab:specific_field[.//ab:id[normalize-space()='commodity' or normalize-space()='cosmetic']]/ab:value

How to append a string on an array?

Lets say I have an array field in a document:
entries: ["hello"]
I would like to append new elements to this array field i.e ["Yellow" ,"Blue"]
I did some research and most the techniques replace the entire record. How can I achieve this?
I'm using javascript.
Check out this question: How to append something to an array?
.
And looks like you are developing using firebase firestore, please take a look at their documentation here.
You'll have to retrieve the document you want to edit first, append the array, then update it.

How to parse value out of column in OpenRefine

I have the results from Google place details. I would like to parse the phone number out of it. How could I do so? I am using OpenRefine and using fetching column on the basis of another column.
Here is an example of Google Place JSON.
As you can see, "formated_phone_number" is a direct child of "result". In Open Refine, you can extract this element using the following GREL formula:
value.parseJson().result.formatted_phone_number
If there are more than one phone number, use a for loop to iterate over the array. The syntax in Open refine is:
forEach(value.parseJson().result, x, x.formatted_phone_number)
(x is a variable name, you can use it or what you want instead.)
The result will be an array. Since an Open Refine column can only display strings, numbers and dates, you must convert the array to string using the join function (and a delimiter of your choice).
forEach(value.parseJson().result, x, x.formatted_phone_number).join('::')

Multiple grep for a single array/ Faceted search

I have a long array with multiple items per object and I have to start to exclude items depending on what someone selects as a check box, you can see a basic idea working here
http://jsfiddle.net/caseybecking/QwtFY/
My question is how do I start to narrow the list without having to do a check on how man y items they have checked, also this doesn't work if they check multiple items per "Fit" or "Wash"
To elaborate further my objectives. I need to store object(s) that only contain the specific item(s) the user wants to FILTER down to. All this is is a ginat filter of multiple pieces of a long array.
Sounds like you want something like .serialize() or .serializeArray() -- then you can send this data to your server or use the serialized array to filter the json object. You'll need to make sure each input element has a name attribute. In the below fiddle I've removed the hideous boxChecked function as haven't a clue what your trying to achieve in that. Anyway:
Fiddle: http://jsfiddle.net/zZtyy/

Categories

Resources