I am trying to read data from firebase using angular 2 and typescript
my code
export class DashboardComponent implements OnInit {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
constructor( afDatabase: AngularFireDatabase) {
this.itemsRef = afDatabase.list('/user_orders/louro');
this.items = this.itemsRef.valueChanges();
this.items.subscribe(val_2 => {
alert(val_2.keys());
val_2.forEach(function (value2) {
{
Object.keys(value2).forEach(function(k2) {
{
// k is key
let count = Object.keys( (value2[k2] )).length;
console.log("New order key "+k2);
for(let i=0;i<count;i++){
console.log(i+"=> "+JSON.stringify (value2[k2][i]));
}
}
});
}
})
});
}
ngOnInit() {
}
}
and on val_2 only contains
[
{
"-L7rtl2NesdOYVD4-bMs": [
{
"ads_show": false,
"brand": "",
"buttonLabel": "Add to cart",
"child": "fruits",
"decrn": "testing for demonstrate",
"key": "-L7rtXc0pMQhi1ClK-pP",
"mid": "fresh fruits",
"note": "",
"orderInfo": {
"message": "nil",
"methode": "cash on delivery",
"status": "nil",
"time": 1521356314040,
"time2": 1521356254115
},
"p_id": 73,
"p_name": "testing",
"position": 0,
"primary_key": "testinglouro",
"qty": {
"m_qty": 1,
"qty": 23,
"unite": "1kg",
"user_intput_qty": 2
},
"quantity_new": 2,
"querykey_shop_name_top": "louro_fruits",
"sellerName": "louro",
"sellonline": true,
"seo": {
"meta_descrption": "",
"meta_title": ""
},
"service": false,
"serviceMessage": "Please enter your complaint details",
"serviceTitle": "Service Requesting",
"shopname": "louro",
"shopview": false,
"summery": "nil",
"tags": "",
"top": "fruits",
"uid": "IG2SxH6Gcabr3QVLz9jE9Wwweh62",
"variants": [
{
"img_position": 0,
"prize": {
"mrp": 58,
"selling_prize": 45,
"tax": 0
},
"qty": {
"m_qty": 1,
"qty": 23,
"unite": "1kg",
"user_intput_qty": 2
},
"shipping": {
"minmumbuy": 0,
"s_cost": 0,
"s_dlts": ""
}
}
],
"variants_position": 0
}
]
}
]
And my database is
I need this key "4X2NpohlbUa3AA7ri6iHGNm2If93" .How to get that key ? I tried val_2.key
but it showing error "[ts] Property 'key' does not exist on type 'any[]'. Did you mean 'keys'?"
In java i am using below code and work fine dataSnapshot1.getKey();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(getResources().getString(R.string.user_orders)+"/"+
shop_name,getContext()));
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
// MainAction.setDefaults("OpenCategories",dataSnapshot.toString(),getActivity());
if (dataSnapshot.hasChildren()) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
orderDetails1 = null;
orderDetails1 = new OrderDetails();
if (dataSnapshot1.hasChildren()) {
int id = -1;
for (DataSnapshot dataSnapshot2 : dataSnapshot1.getChildren()) {
id = id + 1;
if (dataSnapshot2.hasChildren()) {
int productid = -1;
for (DataSnapshot dataSnapshot3 : dataSnapshot2.getChildren()) {
productid = productid + 1;
if (dataSnapshot3.hasChildren()) {
orderDetails1.productmillaList.add(dataSnapshot3.getValue(Productmilla.class));
}
orderDetails1.productmillaList.get(productid).setPosition(Integer.parseInt(dataSnapshot3.getKey()));
orderDetails1.productmillaList.get(productid).setId(dataSnapshot2.getKey());
// Log.d("key2",dataSnapshot2.getKey()+" "+productid);
}
}
//orderDetails1.productmillaList.get(id).setId(dataSnapshot2.getKey());
}
}
orderDetails1.key = dataSnapshot1.getKey();
orderDetails.add(orderDetails1);
}
getaddress(orderDetails);
} else mProgressBar.setVisibility(View.GONE);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
mProgressBar.setVisibility(View.GONE);
Log.w("dd", "Failed to read value."+ error.getMessage());
}
});
Update :
According to the updated response data, you should access using Object.keys(val)[0] which gives the value for key "4X2NpohlbUa3AA7ri6iHGNm2If93", since it is the first key.
According to older response data :
Your val is an array.
This should work.
Object.keys(val[0]).forEach(function(k1) {
{
console.log("Key first : "+ k1);
});
In your JSON, val is an array.
Object.keys(val[0])
will give you the keys of the first element of that array, which is what you're after.
TO get the first key in the Object
Object.keys(val)[0]; //returns 'first key'
[https://stackoverflow.com/a/11509718/7458082][1]
To iterate through the object
Object.keys(obj).forEach(function(key,index) {
// key: the name of the object key
// index: the ordinal position of the key within the object
});
[https://stackoverflow.com/a/11509718/7458082][1]
Related
I need to create a Map dictionary for this two JSON for creating a view like App Store/Play Store so, a view with cell grouped by different heading.
First JSON
[
{
"category_id":"1",
"language_code":"it",
"category_name":"punti d'interesse",
"category_description":"descrizione",
"is_hidden":"0"
},
{
"category_id":"2",
"language_code":"it",
"category_name":"farmacie",
"category_description":"desc farm",
"is_hidden":"0"
}
]
Second JSON
[
{
"poi_id":"1",
"category_id":"1",
"language_code":"it",
"poi_name":"chiese",
"poi_description":"",
"poi_image":"church.jpg"
},
{
"poi_id":"2",
"category_id":"1",
"language_code":"it",
"poi_name":"monumenti",
"poi_description":"",
"poi_image":"monument.jpg"
},
{
"poi_id":"3",
"category_id":"2",
"language_code":"it",
"poi_name":"musei",
"poi_description":"",
"poi_image":"museum.jpg"
}
]
How can I create?
Here you go:
import 'dart:convert';
void main() {
// Your first json
List<Map<String, String>> map1 = [
{
"category_id": "1",
"language_code": "it",
"category_name": "punti d'interesse",
"category_description": "descrizione",
"is_hidden": "0"
},
{
"category_id": "2",
"language_code": "it",
"category_name": "farmacie",
"category_description": "desc farm",
"is_hidden": "0"
}
];
// Your second json
List<Map<String, String>> map2 = [
{
"poi_id": "1",
"category_id": "1",
"language_code": "it",
"poi_name": "chiese",
"poi_description": "",
"poi_image": "church.jpg"
},
{
"poi_id": "2",
"category_id": "1",
"language_code": "it",
"poi_name": "monumenti",
"poi_description": "",
"poi_image": "monument.jpg"
},
{
"poi_id": "3",
"category_id": "2",
"language_code": "it",
"poi_name": "musei",
"poi_description": "",
"poi_image": "museum.jpg"
}
];
// The function that interests you (returns a map in the format you wanted)
Map<String, List> convertData(List<Map> categories, List<Map> pois) {
Map<String, List> result = {};
for (var category in categories) {
List<Map> poisOfCategory = [];
for (var poi in pois) {
if (category['category_id'] == poi['category_id']) {
poisOfCategory.add(poi);
}
}
result[category['category_name']] = poisOfCategory;
}
return result;
}
// Prints the map as a string
print(json.encode(convertData(map1, map2)));
}
The output would be:
{
"punti d'interesse":[
{
"poi_id":"1",
"category_id":"1",
"language_code":"it",
"poi_name":"chiese",
"poi_description":"",
"poi_image":"church.jpg"
},
{
"poi_id":"2",
"category_id":"1",
"language_code":"it",
"poi_name":"monumenti",
"poi_description":"",
"poi_image":"monument.jpg"
}
],
"farmacie":[
{
"poi_id":"3",
"category_id":"2",
"language_code":"it",
"poi_name":"musei",
"poi_description":"",
"poi_image":"museum.jpg"
}
]
}
I am trying to get a value from a JSON file, however it is a complex JSON file and i'm not sure how to go about it.
The value is situated inside an object, that is inside another object, which inside another object.
The values I want to retrieve from the JSON file is "value" and "unit" situated inside the object "metric". I have been able to successfully retrieve the "name" value.
The JSON file:
{
"ingredients": [
{
"name": "breakfast sausage",
"image": "breakfast-sausage-links.jpg",
"amount": {
"metric": {
"value": 226.796,
"unit": "g"
},
"us": {
"value": 8,
"unit": "ounces"
}
}
},
I will also include the method that I parse the JSON from:
private void jsonParse(){
String url = "https://api.spoonacular.com/recipes/"+ rMealId + "/ingredientWidget.json?&apiKey=da7dd16a704f4552b70a96c1e9641b08";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("ingredients");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject ingredients = jsonArray.getJSONObject(i);
String iName = ingredients.getString("name");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.e("Volley", error.toString());
}
});
requestQueue.add(request);
}
Just focusing on your json string, try this:
let ings =
{
"ingredients": [
{
"name": "breakfast sausage",
"image": "breakfast-sausage-links.jpg",
"amount": {
"metric": {
"value": 226.796,
"unit": "g"
},
"us": {
"value": 8,
"unit": "ounces"
}
}
}
]
}
let jp = require('jsonpath');
let eat = jp.query(ings, '$..metric.*');
console.log(eat);
output:
[ 226.796, 'g' ]
From the code below, I want to display request_id, request_title, status, no of trucks in recyclerview using retrofit 2.0. How to do this?
Here is my nested array of objects.
[
{
"id": 96,
"request_id": 24365,
"request_title": "tsfghjjlfdsg;lhj",
"transport_co_id": 1,
"status": 1,
"truck_info": [
{
"driver_mobile": 2147483647,
"driver_name": "dsdsd",
"truck_no": 1111
},
{
"driver_mobile": 2147483647,
"driver_name": "add",
"truck_no": 727
}
]
},
{
"id": 97,
"request_id": "test123",
"request_title": "test title",
"transport_co_id": 1,
"status": 1,
"truck_info": [
{
"driver_mobile": 2147483647,
"driver_name": "dsdsd",
"truck_no": 1111
},
{
"driver_mobile": 2147483647,
"driver_name": "add",
"truck_no": 1
}
]
}
]
create retrofit interface , class
public class ChampApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
I am trying to test my lambda manually with the following dynamodb event input configured in tests -
Let's call this Json-1
{
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
},
"awsRegion": "us-west-2",
"eventName": "INSERT",
"eventSourceARN": eventsourcearn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "2",
"eventVersion": "1.0",
"dynamodb": {
"OldImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"SequenceNumber": "222",
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 59,
"NewImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "MODIFY",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "3",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 38,
"SequenceNumber": "333",
"OldImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "REMOVE",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb"
}
]
}
However, the json of dynamodb items look like this -
Let's call this Json-2
{
"id": {
"S": "RIGHT-aa465568-f4c8-4822-9c38-7563ae0cd37b-1131286033464633.jpg"
},
"lines": {
"L": [
{
"M": {
"points": {
"L": [
{
"L": [
{
"N": "0"
},
{
"N": "874.5625"
}
]
},
{
"L": [
{
"N": "1765.320601851852"
},
{
"N": "809.7800925925926"
}
]
},
{
"L": [
{
"N": "3264"
},
{
"N": "740.3703703703704"
}
]
}
]
},
"type": {
"S": "guard"
}
}
}
]
},
"modified": {
"N": "1483483932472"
},
"qastatus": {
"S": "reviewed"
}
}
Using the lambda function below, I can connect to my table. My goal is create a json which elastic search will accept.
#Override
public Object handleRequest(DynamodbEvent dynamodbEvent, Context context) {
List<DynamodbEvent.DynamodbStreamRecord> dynamodbStreamRecordlist = dynamodbEvent.getRecords();
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
log.info("Whole event - "+dynamodbEvent.toString());
dynamodbStreamRecordlist.stream().forEach(dynamodbStreamRecord -> {
if(dynamodbStreamRecord.getEventSource().equalsIgnoreCase("aws:dynamodb")){
log.info("one record - "+dynamodbStreamRecord.getDynamodb().toString());
log.info(" getting N from new image "+dynamodbStreamRecord.getDynamodb().getNewImage().toString());
String tableName = getTableNameFromARN(dynamodbStreamRecord.getEventSourceARN());
log.info("Table name :"+tableName);
Map<String, AttributeValue> keys = dynamodbStreamRecord.getDynamodb().getKeys();
log.info(keys.toString());
AttributeValue attributeValue = keys.get("Id");
log.info("Value of N: "+attributeValue.getN());
Table table = dynamoDB.getTable(tableName);
}
});
return dynamodbEvent;
}
The format of a JSON item that elastic search expects is this and this is what I want to map the test input json to-
Let's call this Json-3
{
_index: "bar-guard",
_type: "bar-guard_type",
_id: "LEFT-b1939610-442f-4d8d-9991-3ca54685b206-1147042497459511.jpg",
_score: 1,
_source: {
#SequenceNumber: "4901800000000019495704485",
#timestamp: "2017-01-04T02:24:20.560358",
lines: [{
points: [[0,
1222.7129629629628],
[2242.8252314814818,
1254.702546296296],
[4000.0000000000005,
1276.028935185185]],
type: "barr"
}],
modified: 1483483934697,
qastatus: "reviewed",
id: "LEFT-b1939610-442f-4d8d-9991-3ca54685b206-1147042497459511.jpg"
}
},
So what I need is read Json-1 and map it to Json-3.
However, Json-1 does not seem to be complete i.e. it does not have information that a dynamodb json has - like points and lines in Json-2.
And so, I was trying to get a connection to the original table and then read this additional information of lines and points by using the ID.
I am not sure if this is the right approach. Basically, want to figure out a way to get the actual JSON that dynamodb has and not the one that has attribute types
How can I get lines and points from json-2 using java? I know we have DocumentClient in javascript but I am looking for something in java.
Also, came across a converter here but doesn't help me- https://github.com/aws/aws-sdk-js/blob/master/lib/dynamodb/converter.js
Is this something that I should use DynamoDBMapper or ScanJavaDocumentAPI for ?
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html#marshallIntoObjects-java.lang.Class-java.util.List-com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig-
If yes, I am a little lost how to do that in the code below -
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
ScanResult result = dynamoDBClient.scan(scanRequest);
for(Map<String, AttributeValue> item : result.getItems()){
AttributeValue value = item.get("lines");
if(value != null){
List<AttributeValue> values = value.getL();
for(AttributeValue value2 : values){
//what next?
}
}
}
Ok, this seems to work for me.
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
ScanResult result = dynamoDBClient.scan(scanRequest);
for(Map<String, AttributeValue> item : result.getItems()){
AttributeValue value = item.get("lines");
if(value != null){
List<AttributeValue> values = value.getL();
for(AttributeValue value2 : values){
if(value2.getM() != null)
{
Map<String, AttributeValue> map = value2.getM();
AttributeValue points = map.get("points");
List<AttributeValue> pointsvalues = points.getL();
if(!pointsvalues.isEmpty()){
for(AttributeValue valueOfPoint : pointsvalues){
List<AttributeValue> pointList = valueOfPoint.getL();
for(AttributeValue valueOfPoint2 : pointList){
}
}
}
}
}
}
}
I have a Json response which contains multiple arrays from which I need to get the last array. I want to select ExpenseDescriptions and return just this to the ajax call using Linq. ExpenseDescription is always the last array returned. The structure of my response is as follows:
{
"Data": {
"Items": [
{
"Result": {
"Id": "Some Data"
}
},
{
"Result": {
"Id": "Some More Data"
}
},
{
"Result": {
"ExpenseDescriptions": [
{
"Code": "TRH8",
"Description": "Some Description",
"UnitCost": 0,
"MaxThreshold": 0,
"MinThreshold": 0
},
{
"Code": "VVFT3",
"Description": "Some Description",
"UnitCost": 0,
"MaxThreshold": 0,
"MinThreshold": 0
}
]
}
}
]
}
}
I can use linq to do some basic clauses but can't figure out or find a way that lets me do the above. Any help in doing this with Linq would be appreciated.
Here's a solution using Newtonsoft.Json.Linq:
using System;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace test
{
class Program
{
static void Main(string[] args)
{
string json = #"{'Data': {
'Items': [
{
'Result': {
'Id': 'Some Data'
}
},
{
'Result': {
'Id': 'Some More Data'
}
},
{
'Result': {
'ExpenseDescriptions': [
{
'Code': 'TRH8',
'Description': 'Some Description',
'UnitCost': 0,
'MaxThreshold': 0,
'MinThreshold': 0
},
{
'Code': 'VVFT3',
'Description': 'Some Description',
'UnitCost': 0,
'MaxThreshold': 0,
'MinThreshold': 0
}
]
}
}
]
}
}";
JObject jsonobject = JObject.Parse(json);
var last_array = jsonobject.Descendants().Last(x => x.Type == JTokenType.Array);
foreach (var e in last_array)
{
Console.WriteLine(e.ToString());
}
}
}
}
output:
{
"Code": "TRH8",
"Description": "Some Description",
"UnitCost": 0,
"MaxThreshold": 0,
"MinThreshold": 0
}
{
"Code": "VVFT3",
"Description": "Some Description",
"UnitCost": 0,
"MaxThreshold": 0,
"MinThreshold": 0
}