I am trying to order a list by date and popularity. From service i have a json response which has date in string format. Is it possible to change the date from Http get response to date object so that i want to use OrderBy filter to sort the list by date.
My json is
{
"Name": "Paul ",
"Country": "SINGAPORE",
"Date": "12/31/14 20:40",
"Rating": "2"
}
You could pass the date string to the Date Object
Put this in your controller:
myApp.controller("MyPersonController", function($scope){
for (var i in $scope.persons) {
$scope.persons[i].Date = new Date($scope.persons[i].Date);
}
});
If you have problems with formatting, I would recommend a date wrapper like MomentJs
Related
I have following function to get current date:
function() {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat('yyyyMMdd');
var date = new java.util.Date();
return sdf.format(date);
}
And I'm passing the date to JSON file as follows:
* def currDate = getDate()
JSON File:
{
"clientId": "ABC",
"serviceLine": "DSS",
"locationId": "VOL",
"serviceType": "ADC",
"attendanceDate": #(currDate),
"saId": "123",
"attendance": "PRESENT",
"attendanceType": "ATTENDANCE"
}
Json by default converts date into string for e.g. "20230210" (yyyyMMdd). This format is fine & as expected. But just I pass the variable currDate to JSON, it converts it into String with "", e.g., "20230210" but request should have date without quotes, i.e. just 20230210. Otherwise it gives error as Invalid Input.
How can I convert string to date format in JSON file?
After seeing your comments, looks like you are just trying to use the date format output as a number. That's easy, just do this:
* def currDate = 1 * getDate()
Also refer: https://github.com/karatelabs/karate#floats-and-integers
I need to be able to PUT a JSON data from POSTMAN, with no fixed format, store it in database(in MYSQL preferably with datatype: JSON) and then send a GET request to get the same value back. The data can be uniquely identified by an id that I'll be sending as a path variable.
I cannot define an entity class as the JSON will have no fixed format. How do I proceed?
#PutMapping("/sample/{sampleNo}")
public ResponseEntity<Object> addSampleData(
#ApiParam("Sampleto PUT") #PathVariable Long sampleNo, #RequestBody String sampleBody) {
if (sampleBody!= null) {
sampleService.save(new Sample(sampleNo, sampleBody));
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
Not sure how to proceed with the "save" functionality so that the data is stored as a JSON object, because even if I've used datatype JSON n mySQL, GET returns a string with "\".
{
"sampleNo": 1,
"sampleData": "{\"sample\": \"test\", \"sampleNo\": \"20\"}"
}
Example: PUT request body
{
"field1": "test",
"field2": 20
}
GET response body expected
{
"field1": "test",
"field2": 20
}
P.S: I don't need to process the data, so it doesn't matter how I store it, I just need to be able to GET it back in the same format(JSON) it arrived(in the PUT req).
sampleData is returned as JSON String, so it is reasonable to include escape sequence \":
This is a valid JSON String:
String json = "{\"sample\": \"test\", \"sampleNo\": \"20\"}";
This does not compile:
String invalidJson = "{"sample": "test", "sampleNo": "20"}";
Solution:
In order to have the expected response, you have to map your MySQL JSON column into an Object.
There are several ways to achieve this, take a look at a solution here. It maps JSON column into a Map<String, Object>.
I have the following Json file and I need to compare data to see how many times each value repeat itself. The problem is, I have no idea about handling Json. I don't want the answer to my exercise, I want to know how to access the data. Json:
{
"tickets": [
{
"ticket_id": 0,
"timestamp": "2016/05/26 04:47:02",
"file_hash": "c9d4e03c5632416f",
"src_ip": "6.19.128.119",
"dst_ip": "145.231.76.44"
},
{
"ticket_id": 1,
"timestamp": "2017/05/28 16:14:22",
"file_hash": "ce8a056490a3fd3c",
"src_ip": "100.139.125.30",
"dst_ip": "145.231.76.44"
},
{
"ticket_id": 2,
"timestamp": "2015/08/23 03:27:10",
"file_hash": "d17f572496f48a11",
"src_ip": "67.153.41.75",
"dst_ip": "239.168.56.243"
},
{
"ticket_id": 3,
"timestamp": "2016/02/26 14:01:33",
"file_hash": "3b28f2abc966a386",
"src_ip": "6.19.128.119",
"dst_ip": "137.164.166.84"
},
]
}
If this is a string representation of the object, first you need to set a variable and parse the string to have object you can work with.
jsonString = "{...your json string...}"
Then parse the string,
import json
jsonObject = json.loads(jsonString)
To access the data within it's like any other js object. Example :
jsonObject.tickets[0].timestamp
would return "2016/05/26 04:47:02"
tickets is the key within the jsonObject, 0 is the index of the first object in the list of tickets.
You can use the built-in "json" library to parse your file into an object:
import json
f = open('myfile.json','r')
tickets = json.loads(f.read())
This will return a "tickets" object. How you "compare" (or what exactly you compare) is up to you.
I have an AppSync GraphQL API that makes a Query to a DynamoDB and returns a JSON String, however in my Response Mapping Template I use the built-in $util.parseJson() function as listed here - but I'm still returned a JSON string in the Query window and when requesting the data in my React app.
Schema file, I have an ordinary ID & Address field that is of type AWSJSON.
type Venue {
id: ID!
address: AWSJSON
}
When running a mutation, I usually run the address object through a quick JSON.stringify(addressObj) and that formats the object as a string with the \"\" escaped, meaning that it can be inserted into DynamoDB.
Request Mapping template
{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
}
}
Response Mapping template
#set($result = $ctx.result)
## address - parse back to JSON
#set($result.address = $util.parseJson($ctx.result.address))
## Return the result
$util.toJson($result)
The idea to create a new variable and then assign the value to the parseJSON value was taken from How return JSON object from DynamoDB with appsync?. So, as seen below, I am parsing the value through what seems to be the correct method to turn it from stringified JSON, to an object - but it doesn't appear to work.
The current response:
{
"data": {
"getVenue": {
"id": "31538150",
"address": "{\"lng\":-1.54511300000001,\"postcode\":\"LS1 5DL\",\"short\":\"New Station St., LS1\",\"lat\":53.795231,\"full\":\"16 New Station St, Leeds LS1 5DL, UK\"}"
}
}
}
Whereas the response that I am wanting is...
{
"data": {
"getVenue": {
"id": "31538150",
"address": { "lng": -1.54511300000001, "postcode": "LS1 5DL", "short": "New Station St., LS1", "lat": 53.795231, "full": "16 New Station St, Leeds LS1 5DL, UK" }
}
}
}
Any help is greatly appreciated!
Old question but thought I would add some notes...
AWSJSON is a string value that will be parsed into DynamoDB as JSON and stringified again on fetching.
So AppSync expects a string (stringified JSON) for inputs and will return a string that can be parsed with JSON.parse.
However this data is parsed before storing in DynamoDB. So of you query DynamoDB separately than through AppSync then you can query it like it was an object. Same with inputing directly into DynamoDB.
The only way to get JSON in the AppSync result is to define each and every field in GraphQL. Sometimes this can be achieved by restructuring data. For example instead of storing:
{
bob: { age: 34 },
igor: { age: 124 }
}
Which would need to be stringified as an AWSJSON field. It can however be restructured like this:
[{
name: 'bob',
age: 34
}, {
name: 'igor',
age: 123
}]
Which can be defined in GraphQL as something like this:
type User {
age: Int,
name: String
}
So that one can now extract specific value such as age from the GraphQL without involving and JSON parse/stringify.
I have a Json which has so many lines of code. It has a time stamp that can distinguish it from other Json's. I need to take out that time stamp and append it in front of the Json itself. I do not know how to proceed.
For Json. Take any Json with time stamp.
Assuming this is one of your jsons
{"name": "test", "ts": 18928938}
You can simply get the ts, append to another json and then remove the ts if you don't need it.
var result = {};
var json1 = {"name": "test", "ts": 18928938};
result[json1.ts] = json1;
remove result[json1.ts].ts; // This is if you want to remove the original ts