Parse LocalDate json to moment in Angular - json

I'm receiving LocalDate from backend in JSON witch looks like
{"dayOfMonth":25,
"dayOfWeek":"TUESDAY",
"dayOfYear":206,
"month":"JULY",
"monthValue":7,
"year":2017,
"hour":0,
"minute":0,
"nano":0,
"second":0,
"chronology":{"id":"ISO","calendarType":"iso8601"}}}
and I want to parse it to a moment js object
moment(this.car.overview).format();
Invalid date
console.log(moment().format(car.overview));
ERROR TypeError: format.replace is not a function
Anyone knows how to get a valid moment object from this JSON ?

You mean this?
PS: If you receive a JSON string you need to JSON.parse it first
var overview = {
"dayOfMonth": 25,
"dayOfWeek": "TUESDAY",
"dayOfYear": 206,
"month": "JULY",
"monthValue": 7,
"year": 2017,
"hour": 0,
"minute": 0,
"nano": 0,
"second": 0,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
}
console.log(
moment({
y: overview.year,
M: overview.monthValue - 1,
d: overview.dayOfMonth,
h: overview.hour,
m: overview.minute,
s: overview.second,
ms: overview.nano
})
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

what you need to do is
moment().minute(this.car.minute.overview.minute).second(this.car.minute.overview.second).hours(this.car.minute.overview.hour)
and so on for information read https://momentjs.com/docs/#/get-set/

Related

Error on flutter : type 'List<dynamic>' is not a subtype of type 'String'

I have this Json. I want to map my json data. But i get some error.
{
"Id": 0,
"Product_Id": 0,
"Quantity": 0,
"User_Id": "a49a10d2-fc3f-477a-b087-5b0d07545964",
"Active": false,
"CartProducts": null,
"Products": [
{
"Id": 116,
"Shop_Id": 1,
"Offer": 0.0,
"Quantity": 1,
"Price": 100.0,
"Category_Id": 0,
"Description": null,
"Name": "Lacoste Product",
"Active": false,
"Size": "small",
"Color": "black",
"Is_External_Product": true,
"External_Link": "https://www.lacoste.com.tr/urun/kadin-kirmizi-polo-pf0504-007-4/",
"Currency": null,
"ProductImages": null
}
]
}
I am decoding Json here
if(jsonObject['Products']!=null){
productItems = ProductItem.getListFromJson(jsonObject['Products']);
}
static List<ProductItem> getListFromJson(List<dynamic> jsonArray) {
log("getListFromJson");
List<ProductItem> list = [];
for (int i = 0; i < jsonArray.length; i++) {
list.add(ProductItem.fromJson(jsonArray[i]));
}
return list;
}
But i get this error. "[log] type 'List' is not a subtype of type 'String'"
You are trying to assign a value of type String to the product items array. There might be a possibility one of your responses is returning a String and you are expecting an array. Please inspect the response.
It's a JSON parsing issue, and it might not directly related to "Products" as being a list or string.
So instead, for better diagnosing this error, or other error on the future, you could need to enable the dart debugger, while checking the "uncaught exception" option, which will guide you to the broken type casting issue.

Pass dynamic response to Groovy Script Test step as String parameter

I am trying to pass json response to Groovy 'jsonString' parameter. It is correctly working when I pass json manually in code. But my response is dynamic and i need to pass at runtime.
import groovy.json.JsonSlurper
String jsonString = context.expand('${REST SearchRooms#Response}')
JsonSlurper jsonSlurper = new JsonSlurper()
Map convertedJSONMap = jsonSlurper.parseText(jsonString)
if(convertedJSONMap."RoomSearchResult")
{
log.info "ResourceItemID : " + convertedJSONMap."RoomSearchResult"[0]."ResourceItemID"
}
My json response look like this :
{
"Success": true,
"TotalRecords": 2,
"RoomSearchResult": [
{
"ResourceItemID": 2290,
"Name": "Room 23 (L02)",
"LocationId": 7,
"GroupID": 518,
"FloorID": 2,
"DefaultCapacity": 4,
"CanBeBooked": true
},
{
"ResourceItemID": 2063,
"Name": "Room 15 (L10)",
"LocationId": 7,
"GroupID": 518,
"FloorID": 10,
"DefaultCapacity": 8,
"CanBeBooked": true
}
],
"Error": {
"ErrorCode": 0,
"ErrorDescription": ""
}
}
Error : The JSON input text should neither be null nor empty.
I am new to groovy. Please suggest how to do it.
You have trivial error.
Chage below line
From:
String jsonString = context.expand('${REST SearchRooms#Response}')
To:
String jsonString = context.expand('${SearchRooms#Response}')

Want idea JSON parsing using Scala

Below is my JSON format:
{"copybook": {
"item": {
"storage-length": 1652,
"item": [
{
"storage-length": 40,
"level": "05",
"name": "OBJECT-NAME",
"display-length": 40,
"position": 1,
"picture": "X(40)"
},
{
"storage-length": 8,
"occurs-min": 0,
"level": "05",
"name": "C-TCRMANKEYBOBJ-OFFSET",
"numeric": true,
"display-length": 8,
"position": 861,
"occurs": 99,
"depending-on": "C-TCRMANKEYBOBJ-COUNT",
"picture": "9(8)"
}
],
"level": "01",
"name": "TCRMCONTRACTBOBJ",
"display-length": 1652,
"position": 1
},
"filename": "test.cbl"
}}
How can I parse this json and convert it to CSV format? I am using Scala default JSON parser. The main problem I am facing is that I can not use case class to extract the data as all the item names are not same in item array.
This format is ok for me, please follow this link and paste the JSON - https://konklone.io/json/. Any scala code is appreciated. I am getting the below data:
implicit val formats = DefaultFormats
val json2 = parse(jsonString, false) \\ "item"
val list = json2.values.asInstanceOf[List[Map[String, String]]]
for (obj <- list) {
//println(obj.keys)
//obj.values
println (obj.toList.mkString(","))
}
(name,OBJECT-NAME),(storage-length,40),(picture,X(40)),(position,1),(display-length,40),(level,05)
(name,C-TCRMANKEYBOBJ-OFFSET),(storage-length,8),(occurs-min,0),(occurs,99),(picture,9(8)),(position,861),(numeric,true),(depending-on,C-TCRMANKEYBOBJ-COUNT),(display-length,8),(level,05)
https://circe.github.io/circe/ can work better for you in terms of traversing. Just try and read.

Gatling: JsonPath extract multiple values

I'm building a gatling 2.1.3 scenario and I need to extract data from a json body.
Example of the body:
[
{
"objectId": "FirstFoo",
"pvrId": "413"
"type": "foo",
"name": "the first name",
"fooabilities": {
"foo1": true,
"foo2": true
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "firstlogo",
"width": 780,
"height": 490,
"url": "firstlogos/HD/{resolution}.png"
}
]
},
{
"objectId": "SecondFoo",
"pvrId": "414"
"type": "foo",
"name": "the second name",
"fooabilities": {
"foo1": true,
"foo2": false
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "secondlogo",
"width": 780,
"height": 490,
"url": "secondlogos/HD/{resolution}.png"
}
]
}
]
and I have this code trying to extract de data:
exec(
http("get object")
.get(commons.base_url_ws + "/my-resource/2.0/object/")
.headers(commons.headers_ws_session).asJSON
.check(jsonPath("$..*").findAll.saveAs("MY_RESULT"))) (1)
.exec(session => {
foreach("${MY_RESULT}", "result") { (2)
exec(session => {
val result= session("result").as[Map[String, Any]]
val objectId = result("objectId")
val version = result("version")
session.set("MY_RESULT_INFO", session("MY_RESULT_INFO").as[List[(String,Int)]] :+ Tuple2(objectId, version))
})
}
session
})
My goal is:
To extract the objectId and the 9th value from the version array.
I want it to look as Vector -> [(id1, version1),(id2, version2)] in the session to reuse later in another call to the API.
My concerns are:
(1) Is this going to create entries in the session with the complete sub objects? Because in other answers I was that is was always a map that was saved ("id" = [{...}]) and here I do not have ids.
(2) In the logs, I see that the session is loaded with a lot of data, but this foreach is never called. What could cause this ?
My experience in Scala is of a beginner - there may be issues I did not see.
I have looked into this issue: Gatling - Looping through JSON array and it is not exactly answering my case.
I found a way to do it with a regex.
.check(regex("""(?:"objectId"|"version"):"(.*?)",.*?(?:"objectId"|"version"):\[(?:.*?,){9}([0-9]*?),.*?\]""").ofType[(String, String)].findAll saveAs ("OBJECTS")))
I can then use this
foreach("${OBJECTS}", "object") {
exec(
http("Next API call")
.get(commons.base_url_ws + "/my-resource/2.0/foo/${object._1}/${object._2}")
[...]
}

JSON deserialization with GSON error -- This is not a JSON Array

I know there have been a lot of questions on this topic already, but I'm stuck here and I'm sure it's something quite stupid.
I'm parsing a JSON Api that looks like this (renamed & simplified here):
{
"merchant": {
"name": "TestCo",
"id": 108
},
"category": [
{
"merchant_id": 108,
"category_name": "Baby Supplies",
"category_id": 57,
},
{
"merchant_id": 108,
"category_name": "Dining",
"category_id": 59,
}
]}
I have a wrapper class, defined as:
public class WrapperObject {
public MerchantObject merchant;
public List<CategoryObject> category;}
Both merchant & category are properly defined classes of their own. Then I try to deserialize like so:
collectionType = new TypeToken<List<WrapperObject>>() {}.getType();
List<WrapperObject> wrapperObject = new Gson().fromJson(response, collectionType);
This blows up, GSON reports back "This is not a JSON Array".
This worked perfectly right up until last week, when the API changed. The only difference in the JSON was that it used to look like this (note the extra wrapping array around the data):
[{
"merchant": {
"name": "TestCo",
"id": 108
},
"category": [
{
"merchant_id": 108,
"category_name": "Baby Supplies",
"category_id": 57,
},
{
"merchant_id": 108,
"category_name": "Dining",
"category_id": 59,
}
]}]
How do I adjust my code to parse the new JSON? NB, I have no control over the JSON. Thanks!
The square brackets around the old response denote an array (of one element in this case). It looks like the new API returns just a wrapper object, not an array of wrapper objects. Does this work?
wrapperType = new TypeToken<WrapperObject>() {}.getType();
WrapperObject wrapperObject = new Gson().fromJson(response, wrapperType);