How to get JSON into a Freemarker Template (FTL) - json

I've got a MongoDB which I query and the result I serialize and this string I send to my ftl template. Below is the serialized result:
[
{
"id" : "10",
"title" : "Test Title 1",
"partner" : {
"id" : "1",
"name" : "partner 1 ",
"location" : [{
"locationname" : "locationname 1a",
"city" : ""
},{
"locationname" : "locationname 1b",
"city" : ""
}]
}
},
{
"id" : "6",
"title" : "Test Title 2",
"partner" : {
"id" : "1",
"name" : "partner 2 ",
"location" : [{
"locationname" : "locationname 2b",
"city" : ""
}]
}
}
]
How would I use this in my ftl template?
Thanks for any help.

If you really have to serialize before giving the result to FreeMarker... The JSON syntax for maps and lists happens to be a subset of FTL, so assuming the serialized result is in res, res?eval will give you a list of maps.

Related

How to add number as firebase realtime database child without converting to array?

I want to make a DB structure like the following:
But it just converts the whole object to an array.
Here's what I get when I export this JSON.
[ null, {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
}, {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
} ]
I tried keeping the first child as 1 and the second as "first" (or some random string) and exported JSON and here's what I got...
{
"1" : {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
},
"first" : {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
}
}
I want something similar, but with children like "1", "2" instead.
Is it possible? Or using children like "first", "second" is the only solution right now?
When you read a node with sequential numeric keys (as in your screenshot and first JSON example), the Firebase SDK (and REST) API) automatically coerce that data to an array. There is no way to configure this behavior.
If you don't want the array coercion you should use non-numeric keys. My common approach is to prefix each key with a short string, like "key1", "key2", etc.
Also see: Best Practices: Arrays in Firebase.

jq - how can I flat my list of lists into one level list

how can I have transformed my json
{
"clients": [
{
"id" : "qwerty",
"accounts" : [{"number" : "6666"}, {"number" : "7777"}]
},
{
"id" : "zxcvb",
"accounts" : [{"number" : "1111"}, {"number" : "2222"}]
}
]
}
into following type of json? using JQ
{
"items": [
{
"id" : "qwerty",
"number" : "6666"
},{
"id" : "qwerty",
"number" : "7777"
},{
"id" : "zxcvb",
"number" : "1111"
},{
"id" : "zxcvb",
"number" : "2222"
}]
}
What kind of tools from JQ can help me? I can't choose any possible way to do it
Something like this should do the trick:
{items: [.clients[] | {id} + .accounts[]]}
Online demo

Convert Json - Integers without quotes, Strings with quotes

I'm reading a CSV file with some strings and some integer columns and converting them to JSON. While this conversion, all fields and values seem to have double quotes around them. However I want the integer values NOT to have double quotes.
I'm using Jackson Fasterxml and here's my code snippet
File input = new File("/Users/name/1.csv");
File output = new File("/Users/name/output.json");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
// Read data from CSV file
List<Object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
// Write JSON formated data to output.json file
mapper.writerWithDefaultPrettyPrinter().writeValue(output, readAll);
Here's my expected output: Output, please note id and budget do not have double quotes on them
[ {
"id" : 120,
"name" : "Name 1",
"type" : "type1",
"budget" : 100
},
{
"id" : 130,
"name" : "Name 2",
"type" : "type2",
"budget" : 200
},
{
"id" : 140,
"name" : "Name 3",
"type" : "type2",
"budget" : 130
},
{
"id" : 150,
"name" : "Name 4",
"type" : "type4",
"budget" : 400
}
}]
However all fields and values have quotes after conversion
[ {
"id" : "120",
"name" : "Name 1",
"type" : "type1",
"budget" : "100"
},
{
"id" : "130",
"name" : "Name 2",
"type" : "type2",
"budget" : "200"
},
{
"id" : "140",
"name" : "Name 3",
"type" : "type2",
"budget" : "130"
},
{
"id" : "150",
"name" : "Name 4",
"type" : "type4",
"budget" : "400"
}
}]
Unfortunately right now it is not possible to read CSV and specify type for using schema only. You can create POJO with private int budget; and conversion will be done automatically. For other solutions take a look on this question: jackson-dataformat-csv: Mapping number value without POJO where you can see:
Usage of univocity-parsers
Trick with custom Map implementation.

UISearchBar and JSON Data

This is my first time using a UISearchBar and it is proving to confuse me.
I think my biggest problem here is not understanding how JSON is stored and displayed.
I have my JSON data stored as:
[{
"referralId" : "v-1519014616",
"name" : "Empire State Building",
"storeId" : "",
"hereNow" : {
"count" : 2,
"summary" : "2 people are here",
"groups" : [
{
"count" : 2,
"type" : "others",
"name" : "Other people here",
"items" : [
]
}
]
},
"stats" : {
"tipCount" : 1105,
"checkinsCount" : 185916,
"usersCount" : 129661
},
"venueRatingBlacklisted" : true,
"beenHere" : {
"lastCheckinExpiredAt" : 0
},
"specials" : {
"count" : 0,
"items" : [
]
},
"venuePage" : {
"id" : "64514349"
},
"verified" : true,
"location" : {
"state" : "NY",
"neighborhood" : "Midtown Manhattan, New York, NY",
"crossStreet" : "btwn 33rd & 34th St",
"lat" : 40.748469532965927,
"address" : "350 5th Ave",
"cc" : "US",
"city" : "New York",
"postalCode" : "10118",
"formattedAddress" : [
"350 5th Ave (btwn 33rd & 34th St)",
"New York, NY 10118"
],
"lng" : -73.985513794430119,
"distance" : 17,
"country" : "United States"
},
"hasPerk" : false,
"id" : "43695300f964a5208c291fe3",
"categories" : [
{
... etc
},
I have this stored in an array titled locations and I am trying to filter through it with a search bar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let name = self.locations[0]["name"].string
filteredLocations = locations.filter { names in
return (name!.lowercased().contains(searchText.lowercased()))
}
tableView.reloadData()
}
Now, obviously, name is only going to search for the item in the array. How would I be able to filter through every location's name key to properly filter?
try this one ... don't take name first from location array...
let filterArr = locations.filter {
return (($0["name"] as! String).lowercased().contains(searchText.lowercased())))
}
use this one to get the search result

Push data in json array MongoDB

I have the following structure in my mondoDB:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0",
I want to add another json to the source array for the following resut:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
},
{
"source_id" : "800000022",
"source" : "Test2"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0"
What will be MongoDB query for it?
Read about $push here
Your code will something like this
yourmodelname.update({ /* some conditions to updating files */ },{$push: {"source": {"source_id" : "213123", "source" : "asdasd"}}})