Parse JSON array file with JSONPATH - arrays

I want to parse this with JSONPath:
[
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
]
Can you help with that please?

If the object is:
[
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
]
Then "$[0]" will return:
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]
And "$[1]" will return:
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
You can do it two levels deep as well. "$[0][4]" will return:
205
You can also extract the elements of the array into a list with "$[*]", which will return a list of 2 elements. The first being:
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]
and the second being:
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]

Using DefiantJS, you can search a JSON structure with XPath syntax. This library extends the global object JSON with a search function.
In this scenario, you can write something like this;
var data = [
[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
],
search = JSON.search( data, '//*/*/*' );
Check out this fiddle; http://jsfiddle.net/hbi99/5NfeM/

This works for me
JsonPath.with(jsonResponse).param("name", "getName").get("findAll { a -> a.name == name }")

Related

Map the Array in to the each individual JSON so the records can be created

I am trying to convert the below Array in to the JSON, trying by iterating through the Array and transforming. The array looks like below
Quote_Sent_To__r = [
{
Quote__c=0Q02D05XGQSA2,
Id=a1H2D0m94QUAQ,
type=QuoteSentTo__c
},
{
Quote__c=0Q02D00XGQSA2,
Id=a1H2D00000AQ,
type=QuoteSentTo__c
}
]
I have stored the array in to the variable quoteSentToList and iterating through the for loop
Within each iteration I need to get the JSON like
{
"Quote__c": "0Q02D05XGQSA2"
}
So this can be passed to the Salesforce Update operation. I tried like
%dw 2.0
output application/json
var item = vars.quoteSentToList[counter]
---
{
"Quote__c" :payload.Id
}
It errors saying
Reason: Unable to resolve reference of: counter..
Scripting language error on expression 'payload'. Reason: Unable to resolve reference of: payload..
This is my first project and any help is greatly appreciated
Error
""Unexpected character 'v' at quoteSentToList#[1:1] (line:column), expected false or true or null or {...} or [...] or number but was , while reading quoteSentToList as Json.
1| vars.existingQuote[0].Quote_Sent_To__r ^" evaluating expression: "%dw 2.0 output application/json
---
vars.quoteSentToList map { Quote__c: payload.Id, Id: $.Id }"."
counter is a Mule variable, not a DataWeave variable. You need to use the prefix vars. to reference it inside DataWeave scripts: vars.counter.
Alternatively, instead of using a <foreach> scope, you can transform the entire array at once and then use each element as needed:
%dw 2.0
output application/json
---
vars.quoteSentToList map { Quote__c: $.Id }
Output:
[
{
"Quote__c": "a1H2D0m94QUAQ"
},
{
"Quote__c": "a1H2D00000AQ"
}
]

There is a way to loop throught nested arrays like Matrioska, added dynamically?

I'm trying to declare a function into my Angular typescript class that could help me to loop throught nested arrays that are added dynamically because my template show a custom Kendo grid so I have this problem. I need to loop throught a type of Matrioska that everytime I add a grouping by the name column automatically inside the array inside the first generic array there is a second array and go on.
The structure is like this:
array:[
obj1:{
array:[
obj:{
--the information that I want--
}
]
}
obj2:{
array:[
obj:{
--the information that I want--
}
]
}
obj3:{
array:[
obj:{
--the information that I want--
}
]
}
..............
AND SO ON
]
I could use a recorsive method?
Can anyone help me? Thanks in advance for the help!
You could use:
for(let first of response) { -> in array
console.log(first) -> gives obj1.
for(let second of first.array) { -> in array1
console.log(second) -> gives obj.
}
}

Full Text Search in OrientDB JSON Data

I have following data in OrientDB 3.0.27 where some of the values are in JSON Array and some are string
{
"#type": "d",
"#rid": "#57:0",
"#version": 2,
"#class": "abc_class",
"user_name": [
"7/1 LIBOR Product"
],
"user_Accountability": [],
"user_Rollout_32_date": [],
"user_Brands": [
"AppNet"
],
"user_lastModificationTime": [
"2019-11-27 06:40:35"
],
"user_columnPercentage": [
"0.00"
],
"user_systemId": [
"06114a87-a099-0c30c60b49c4"
],
"user_lastModificationUser": [
"system"
],
"system_type": "Product",
"user_createDate": [
"2017-10-27 09:58:42"
],
"system_modelId": "bian_model",
"user_parent": [
"a12a41bd-af6f-0ca028af480d"
],
"user_Strategic_32_value": [],
"system_oeId": "06114a87-a099-0c30c60b49c4",
"user_description": [],
"#fieldTypes": "user_name=e,user_Accountability=e,user_Rollout_32_date=e,user_Brands=e,user_lastModificationTime=e,user_columnPercentage=e,user_systemId=e,user_lastModificationUser=e,user_createDate=e,user_parent=e,user_Strategic_32_value=e,user_description=e"
}
I have tried following queries:
select * from `abc_class ` where any() = ["AppNet"] limit 2;
select * from `abc_class ` where any() like '%a099%' limit 2;
Both of the above queries work since they are respecting the datatype of the field.
I want to run a contains query which will search in ANY field with ANY data type (like String, number, JSON Array, etc) more of like a - full text search.
select * from `abc_class ` where any() like '%AppNet%' limit 2;
The above query doesn't work since the real value is inside JSON Array. Tried almost all the things from filtering section documentation
How can I achieve full-text search like functionality with the existing data?
EDIT # 1
After doing more research now I'm able to atleast convert the array value into string and then run like operator on it, like below;
select * from `abc_class` where user_name.asString() like '%LIBOR%'
However, using any().asString() doesn't result any result
select * from `abc_class` where any().asString() like '%LIBOR%'
If the above query can be enhanced somehow to query any column as string, then the problem can be resolved.
If all the column values needs to be searched then we can create a JSON object of the full row data and convert it into String.
Then query the string with like keyword, as follows:
select * from `abc_class` where #this.toJSON().asString() like '%LIBOR%'
If we will be converting to #this.asString() directly then we'll be getting the count of array elements instead of the real data inside the array elements like below:
abc_class#57:4{system_modelId:model,system_oeId:14f4b593-a57d-4d37ad070a10,system_type:Product,user_lastModificationUser:[1],user_name:[1],user_description:[0],user_Accountability:[0],user_lastModificationTime:[1],user_Rollout_32_date:[0],user_Strategic_32_value:[0],user_createDate:[1],user_Brands:[0],user_parent:[1],user_systemId:[1],user_columnCompletenessPercentage:[1]} v2
Therefore, we need to first convert into JSON and then into String to query the full record using #this.toJSON().asString()
References:
https://orientdb.com/docs/last/sql/SQL-Methods.html
https://orientdb.com/docs/last/sql/SQL-Where.html
https://orientdb.com/docs/last/sql/SQL-Syntax.html

Parse through multidemensional array in 'Swift'

I can't find out how to write a multidemensional array using 'Swift' and parse it. The sample code below is what I need it to do. The sample is written in JavaScript because I can't find the correct syntax in Swift but that is what I need to convert it to:
var array = [{
name:'Steve',
dates:[
'10-29-2016',
'11-03-2016',
]
},{
name:'Bill',
dates:[
'08-13-2016',
'01-20-2016',
]
}
]
console.log(array[0].name)
// logs 'Steve'
console.log(array[0].dates[])
// logs '11-03-2016'
When I tried to write it in Swift I get a lot of syntax errors and from my research I am unable to find examples using this array syntax. Any ideas? Thanks in advance!
Here I'm creating a list of tuples, and the tuple consists of
1) name (of type String) and 2) dates (of type List of Strings).
var arr:[(name: String, dates: [String])] = [
(name: "Steve", dates: ["10-29-2016", "11-03-2016"]),
(name: "Bill", dates: ["10-29-2016", "11-03-2016"])
]
for tup in arr {
print(tup.name)
print(tup.dates)
}
import UIKit
var array = [
["name":"Steve",
"dates":[
"10-29-2016",
"11-03-2016"
],
],
[
"name":"Bill",
"dates":[
"08-13-2016",
"01-20-2016"
]
]
]
print(array[0])
print(array[0]["name"])

Json creation in ruby for a list

I am new to Ruby.
I want to create a JSON file for a group of elements.
For this, I am using eachfunction to retrieve the datas. I want to create json as follows for the 4 length array,
'{
"desc":{
"1":"1st Value",
"2":"2nd value"
"3":"3rd Value",
"4":"4th value"
},
}'
This is my array iteration,
REXML::XPath.each( doc, "//time" ) { |element1|
puts element1.get_text
}
I know here is the simple code to generate a JSON,
require 'json/add/core'
class Item < Struct.new(:id, :name); end
chair = Item.new(1, 'chair')
puts JSON.pretty_generate(chair)
This syntax will generate a json as follows,
{
"json_class": "Item",
"v": [
1,
"chair"
]
}
But I'm not sure how to do that to make JSON for my elements as stated above. Google search didn't give me a proper way to do this.
Can anyone help me here?
it means this?:
require 'json'
my_arr= ["1st Value","2nd Value","3rd Value","4th Value"]
tmp_str= {}
tmp_str["desc"] = {}
my_arr.each do |x|
tmp_str["desc"]["#{x[0]}"] = x
end
puts JSON.generate(tmp_str)
you can iterate the string array ,then take the strings to hash object.JSON can easy to parse Hash objcect .

Resources