Convert Java Object into JSON Object - json

We are using Airbyte to sync MongoDB data into Snowflake. For some reason, our JSON lists are synched as what appear to be Java Objects.
I am trying to get the data into a JSON format, so I can work with the properties.
Example of Java object row in our cost column:
"[Document{{currency=USD, value=815.00}}, Document{{currency=EUR, value=671.00}}, Document{{currency=GBP, value=579.00}}, Document{{currency=DKK, value=4992.00}}, Document{{currency=SEK, value=6760.00}}]"
I want to convert the row into the following format
[{
"currency": "USD",
"value": 815.00
}, {
"currency": "EUR",
"value": 671.00
}, {
"currency": "GBP",
"value": 579.00
}, {
"currency": "DKK",
"value": 4992.00
}, {
"currency": "SEK",
"value": 6760.00
}]
How can I accomplish that in Snowflake?

You can use regex but it's not the most elegant way:
set term='[Document{{currency=USD, value=815.00}}, Document{{currency=EUR, value=671.00}}, Document{{currency=GBP, value=579.00}}, Document{{currency=DKK, value=4992.00}}, Document{{currency=SEK, value=6760.00}}]';
select regexp_replace(regexp_replace(regexp_replace($term, 'Document',''), '}}', '}'), '{{', '{');
I get:
[{currency=USD, value=815.00}, {currency=EUR, value=671.00}, {currency=GBP, value=579.00}, {currency=DKK, value=4992.00}, {currency=SEK, value=6760.00}]
This can be further extended to replace currency with "currency".

Related

Angular JSON pipe with angular-datatables

I'm using angular-datatables to display NoSQL de-normalized data in grid for visualization purpose,
I have few complex nested json objects and wanted to display specific cell with prettified json with inbuilt JsonPipe
I'm using datatables with data binding as
<table id="dtTable" datatable [dtOptions]="data"></table>
Sample JSON
[
{
"Id": "1",
"Name": "Test",
"Account": {
"Id": "12",
"Name": "Stackoverflow",
"Contact": {
"Id": "23",
"Name": "stack exchange",
"Phone1": "712426",
"Phone2": "490591",
"Address": {
"Id": "12",
"Name": "Address 1",
"AddressType": "commercial"
}
}
},
"CreatedBy": {
"Id": "123",
"Name": "User 1"
},
"CreatedDate": "2022-04-11T10:42:28.7525823Z",
"ModifiedBy": {
"Id": "124",
"Name": "User 2"
},
"ModifiedDate": "2022-04-11T10:42:28.7525823Z"
},
{
...
},
...
]
want to render as
Id
Name
Account
Created By
1
Test
{ Pretified JSON}
{Json}
Do we have any option to render entire json content in specific column cell of tables using angular-datatables? or do we have any other option other than json pipe to display formatted json content in angular-datatables
Yes, you can use the build in pipe as you mentioned:
In HTML directly:
...
<td>{{accountInfo | json}}</td>
...
Or in TS file using the transform function.
...
this.accountInfo = this.jsonPipe.transform(response.accountInfo)
...
If you use the transform function, be sure that the Pipe is imported properly in the TS file.

Robot Framework how to count item list in JSON

I would like to count item "Start" from JSON APIs on Robot Framework
{
"result": {
"api": "xxx",
"timestamp": "14:41:18",
"series": [
{
"series_code": "test",
"series_name_eng": "test",
"unit_eng": "t",
"series_type": "e",
"frequency": "s",
"last_update_date": "2020",
"observations": [
{
"start": "2020-01",
"value": "999"
},
{
"start": "2020-02",
"value": "888"
},
{
"start": "2020-03",
"value": "777"
},
]
}
I use this not working
${json_string} Get File ./example.json
${json_object} evaluate json.loads('''${json_string}''') json
#${value}= get value from json ${json_object} $.result.series[0].observations
${x_count} Get Length ${json_object["$.result.series[0].observations"]}
Could you please help guide to for how?
The Json provided in the example above is not valid one. That will need to be fixed by closing series array ] then close the results object } and then close the outer object }
Valid json will look like this -
${Getjson}= {"result":{"api":"xxx","timestamp":"14:41:18","series":[{"series_code":"test","series_name_eng":"test","unit_eng":"t","series_type":"e","frequency":"s","last_update_date":"2020","observations":[{"start":"2020-01","value":"999"},{"start":"2020-02","value":"888"},{"start":"2020-03","value":"777"}]}]}}
You were close with this jsonpath $.result.series[0].observations. The correct one is in below example -
${json}= Convert String to JSON ${Getjson}
#{Start}= Get Value From Json ${json} $.result.series[?(#.observations)].observations[?(#.start)].start
${length} Get length ${Start}
log ${length}
Output:-

Regex to extract UUID without key value in json

I have below json from which I wants to extract '07199fca-b43f-4e58-b0fc-c1e254f34ac0' values. I got with multiple regex available in regex101 but every time I get all UUID value any way by which I get Unique value that has no json key assign.
JSON Syntax:
[
[
{
"clientId": 178,
"uniqueId": "5f7f919f-7e0f-4a1e-9a91-89b673896da6",
"displayName": "Automation Client Test",
"productVersion": "7.9.0.0"
},
{
"clientId": 1206,
"uniqueId": "096b3549-6899-4621-854c-e682aeb543bd",
"displayName": "TestClient1",
"productVersion": "7.9.0.0"
},
{
"clientId": 1356,
"uniqueId": "faad0e20-dd29-4146-8a8f-37648749aa4e",
"displayName": "Client Automation",
"productVersion": "7.9.0.0"
}
],
"07199fca-b43f-4e58-b0fc-c1e254f34ac0"
]
From your question it is not 100% clear what you are looking for, but if you are looking for the last element of the list that also satisfies [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} regex (with quotes around it), then:
(?:['"])([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(?:['"]\s*]\s*$)
See Regex Demo
(?:['"]) Matches ' or ".
([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) Your regex captured in capture group 1.
(?:['"]\s*]\s*$) Matches ' or " followed by 0 or more white space characters followed by a ] followed by 0 or more white space characters followed by the end of string.
The code:
import re
s = """ [
[
{
"clientId": 178,
"uniqueId": "5f7f919f-7e0f-4a1e-9a91-89b673896da6",
"displayName": "Automation Client Test",
"productVersion": "7.9.0.0"
},
{
"clientId": 1206,
"uniqueId": "096b3549-6899-4621-854c-e682aeb543bd",
"displayName": "TestClient1",
"productVersion": "7.9.0.0"
},
{
"clientId": 1356,
"uniqueId": "faad0e20-dd29-4146-8a8f-37648749aa4e",
"displayName": "Client Automation",
"productVersion": "7.9.0.0"
}
],
"07199fca-b43f-4e58-b0fc-c1e254f34ac0"
]"""
m = re.search(r"""(?:['"])([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(?:['"]\s*]\s*$)""", s, flags=re.DOTALL|re.IGNORECASE)
if m:
print(m[1])
Prints:
07199fca-b43f-4e58-b0fc-c1e254f34ac0
JSON is a well-structured data, using regular expressions for parsing JSON is not the best idea, I would rather recommend going for JSON Extractor instead
The relevant JsonPath query would be as simple as $[1]
Demo:
More information: API Testing With JMeter and the JSON Extractor

Parsing non consistent json response length in excel vba

Using VBA-JSON v2.0.1 in excel VBA
Here is the JSON response I get from API query
{
"currency": {
"code": "USD",
"name": "US Dollar",
"prefix": "$",
"postfix": null
},
"products": [
{
"product_id": xxxxx,
"model_code": "xxxxx",
"quantity": 1,
"price": "45.60",
"total": "45.60",
"retail_price": "63.84"
}
],
"shipping": [
{
"name": "UPS",
"price": 43.83,
"delivery": "3 -10 Days delivery"
},
{
"name": "DHL",
"price": 20.29,
"delivery": "2-6 days"
},
{
"name": "FedEx",
"price": 31.46,
"delivery": "2-6 days"
},
{
"name": "EMS",
"price": 25.74,
"delivery": "7 - 25 Days delivery"
},
{
"name": "Air Mail",
"price": 11.85,
"delivery": "10 - 25 Days delivery"
}
]
}
Here is a part of my code to parse the price from "Air Mail" element.
result = objHTTP.responseText
Dim Json As Object
Dim resultAirmailprice As String
Set Json = JsonConverter.ParseJson(result)
resultAirmailprice = Json("shipping")(5)("price")
Cells(2, 2).Value = resultAirmailprice
The code runs fine when the "Air Mail" element is in (5) of "shipping" element. The problem is sometimes there are no "UPS" and "Air Mail" elements, so I got an error.
How to write code to parse the "Air Mail" price and if not exists, parse from "EMS" price(or the cheapest price out of all)?
Looking at Json parser code, it returns a Dictionary Object that contains other Dictionary Objects (sub keys) and, for arrays, Collection Objects. In the Json I see that "shipping" is an array and so the parser returns a Collection Object.
So you can use all the Collection members and methods to manipulate and access it. In particular, you can use Json("shipping").Count to check how many elements the Shipping collection has. Or you can iterate over the collection with For each x in Json("shipping").
To check whether you have a Dictionary or a Collection, you could use the TypeName function or the TypeOf..Is operator.

Can we add array of objects in amazon cloudsearch in json format?

I am trying to create a domain and uploading a sample data which is like :
[
{
"type": "add",
"id": "1371964",
"version": 1,
"lang": "eng",
"fields": {
"id": "1371964",
"uid": "1200983280",
"time": "2013-12-23 13:00:26",
"orderid": "1200983280",
"callerid": "66580662",
"is_called": "1",
"is_synced": "1",
"is_sent": "1",
"allcaller": [
{
"sno": "1085770",
"uid": "1387783883.30547",
"lastfun": null,
"callduration": "00:00:46",
"request_id": "1371964"
}
]
}
}]
when I am uploading sample data while creating a domain, cloudsearch is not taking it.
If I remove allcaller array then it takes it smoothly.
If cloudsearch does not allowing object arrays, then how should I format this json??
Just found after searching on aws forums, cloudsearch doesnot allow nested json (object arrays) :(
https://forums.aws.amazon.com/thread.jspa?messageID=405879&#405879
Time to try Elastic search.