AttributeError: 'str' object has no attribute 'items' error - json

I am writing a python script for extracting information from the json file. I am printing the title of the book with lastname as Marcus. I have the output but it has the error AttributeError: 'str' object has no attribute 'items' error as well
import json
from pprint import pprint
with open('bibliography.json.txt', encoding='utf-8') as data_file:
data = json.load(data_file)
for entry in data['bibliography']['biblioentry']:
for authors in entry['author']:
for key,val in authors.items():
if(key== 'lastname' and val=='Marcus'):
title=entry['title']
print(title)
the json file looks like this:
{
"bibliography": {
"biblioentry": [
{
"-type": "Journal Article",
"title": "A brief survey of web data extraction tools",
"author": [
{
"firstname": "Alberto",
"middlename": "HF",
"lastname": "Laender"
},
{
"firstname": "Berthier",
"middlename": "A",
"lastname": "Ribeiro-Neto"
},
{
"firstname": "Altigran",
"middlename": "S",
"lastname": "da Silva"
},
{
"firstname": "Juliana",
"middlename": "S",
"lastname": "Teixeira"
}
],
"details": {
"journalname": "ACM Sigmod Record",
"volume": "31",
"number": "2",
"pages": "84-93"
},
"year": "2002",
"publisher": "ACM"
},......

I think it is because it interprets the json file as a string. I think you might want to see this if it helps you:
Extract data from JSON API using Python

Related

Groovy - Parse JSON where only certain values exists in response

I am trying to parse a JSON response that has repeating objects with JsonSlurper to compare to a JDBC query. However, I only want to compare objects where a certain values exist within that object.
If I had a response that looks like this, how would I only parse the objects where the country equals USA or Canada, therefore ignoring anything else?
{
"info": [{
"name": "John Smith",
"phone": "2125557878",
"country": {
"value": "USA"
}
},
{
"name": "Jane Smith",
"phone": "2125551212",
"country": {
"value": "USA"
}
},
{
"name": "Bob Jones",
"phone": "4165558714",
"country": {
"value": "Canada"
}
},
{
"name": "George Tucker",
"phone": "4454547171",
"country": {
"value": "UK"
}
},
{
"name": "Jean Normand",
"phone": "4454547171",
"country": {
"value": "France"
}
}]
}
This is what I have in groovy:
def jsonResponse = context.expand('${RESTRequest#Response}')
def parsedJson = new JsonSlurper().parseText(jsonResponse)
def info = parsedJson.info
def jsonDataObjects = []
info.each { json ->
jsonDataObjects.add(Model.buildJSONData(json))
}
I am building a collection of the elements that I need to compare to a database. How do I only add to that collection where the info.country.value = USA or Canada?
I tried using .findAll like this just to test if I could get it to filter by just one of the countries:
def info = parsedJson.info.country.findAll{it.value == "USA"}
But, when I do that, only the value field is kept. I lose the name and phone from the parse.
Thanks in advance for any assistance.
Did you try
def info = parsedJson.info.findAll{it.country.value == "USA"}
?

How to combine columns in spark as a JSON in Scala

I have a variable which is constructed as follows extracting data using Spark SQL:
{
"resourceType" : "Test1",
"count" : 10,
"entry": [{
"id": "112",
"gender": "female",
"birthDate": 1213999
}, {
"id": "urn:uuid:002e27cf-3cae-4393-89c5-1b78050d9428",
"resourceType": "Encounter"
}]
}
I want the output in the following format:
{
"resourceType" : "Test1",
"count" : 10,
"entry" :[
"resource" :{
"id": "112",
"gender": "female",
"birthDate": 1213999
},
"resource" :{
"id": "urn:uuid:002e27cf-3cae-4393-89c5-1b78050d9428",
"resourceType": "Encounter"
}]
}
I am basically new to Scala :), would need help in this.
EDIT: Adding the scala code to create the JSON:
val bundle = endresult.groupBy("id").agg(count("*") as "total",collect_list("resource") as "entry").
withColumn("resourceType", lit("Bundle")).
drop("id").
select(to_json(struct("resourceType","entry"))).
map(row => row.getString(0).
replace("\"entry\":[\"{", "\"entry\":[{").
replace("}\"]}","}]}"). // Should be at the end of the string ONLY (we might switch to regex instead
replace("}\",\"{","},{")
replace("\\\"", "\"")
)

Binding nested JSON data in SAPUI5 XML view

I am trying to bind data from a nested JSON file in my SAPUI5 application. The view is in the XML format.
Here is the snippet from my JSON file:
{
"Departments": [
{
"ID": "1",
"Name": "Транспортный цех 1",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
},
{
"ID": "2",
"Name": "Транспортный цех 2",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
}
]
}
I am loading the JSON file in my controller and then binding the data "Address" and "Name" in my XML view as follows:
<List id="list1" items="{path:'/Departments'}">
<items>
<ObjectListItem icon="{Logo}" type="Active" press="onListItemPress" number="{Count}" title="{Name}">
<attributes>
<ObjectAttribute text="{Address}" />
</attributes>
</ObjectListItem>
</items>
</List>
However when I tried binding the nested data "FirstName" or "LastName" like this I am not able to bind it.
text="{Employees/LastName}"
Employees is an array.
You can pick one entry and use {Employees/0/LastName} if you like.
You can also use a formatter function to merge the employees to a string:
View:
<ObjectAttribute text="{path: 'Employees', formatter: '.formatEmployees'}"/>
Controller:
formatEmployees: function(aEmployees){
return aEmployees.map(function(employee){ return employee.LastName + ", " + employee.FirstName; }).join("; ");
}
You can use a list-control like sap.m.ListBox or sap.m.Tokenizer and bind the items to the Employees array.

Reading xml or json ...multiple line into single string

I want to read json or xml file in pyspark.lf my file is split in multiple line in sc.textFIle(json or xml)
Input
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
Its in multiple line
Output
{"employees:[{"firstName:"John",......]}
Every think in one string or one line..
In pyspark
Please help me I am new to spark
If you have access to the dictionary file (I'm not familiar with PySpark, but superficially, it seems you do) you can use a standard JSON library to "pretty print" it:
>>> import json
>>> my_dict = {'4': 5, '6': 7}
>>> print json.dumps(my_dict, sort_keys=True,
... indent=4, separators=(',', ': '))
{
"4": 5,
"6": 7
}
https://docs.python.org/2/library/json.html

creating JSON object without using a list

I want to be able to create a JSON object so that I can access it like this.
education.schools.UNCC.graduation
Currently, my JSON is like this:
var education = {
"schools": [
"UNCC": {
"graduation": 2015,
"city": "Charlotte, NC",
"major": ["CS", "Spanish"]
},
"UNC-CH": {
"graduation": 2012,
"city": "Chapel Hill, NC"
"major": ["Sociology", "Film"]
}
],
"online": {
"website": "Udacity",
"courses": ["python", "java", "data science"]
}
};
When I go to Lint my JSON, I get an error message.
I know I can reformat my object to access it like this (below), but I don't want to do it this way. I want to be able to call the school name, and not use an index number.
education.schools[1].graduation
Objects have named keys. Arrays are a list of members.
Replace the value of "schools" with an object. Change [] to {}.
This is your JSON corrected.
Your JSON is invalid.
{
"schools": [
{
"UNCC": {
"graduation": "2015",
"city": [
"CS",
"Spanish"
],
"major": [
"CS",
"Spanish"
]
}
},
{
"UNC-CH": {
"graduation": "2012",
"city": [
"Chapel Hill",
"NC"
],
"major": [
"Sociology",
"Film"
]
}
}
],
"online": {
"website": "Udacity",
"courses": [
"python",
"java",
"data science"
]
}
}
Explanation:
"city": "Chapel Hill, NC" -> this is a array with 2 values "Chapel Hill" and "HC", like you do with major and courses.
The Schools array, you need to use this sintaxe to construct a array [{
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html