-1

I have this json output stored in a dict. I want to remove "u" from it and also the key "social_tone" and "Social Tone". below is my dictionary.

     {u'document_tone': {u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.573181, u'tone_id': u'anger'})
  • This will help you understand better. https://stackoverflow.com/questions/13940272/python-json-loads-returns-items-prefixing-with-u – Bernard 'Beta Berlin' Parah Sep 07 '17 at 06:32
  • 1
    The `u` isn't a actually *contained* in the dictionary, it is merely how *unicode strings* are represented. – juanpa.arrivillaga Sep 07 '17 at 06:49
  • You will have to provide more context to receive a good answer on how to remove the key "social_tone". What exactly do you want to remove? Does the dict always have the same structure? I suggest asking a separate question about removing "social_tone" containing both expected input and expected output. – Daniel Hepper Sep 07 '17 at 07:06

3 Answers3

5

This is not JSON, this is the string representation of a Python dictionary. You are probably doing something like str(my_dict).

To encode your dictionary into proper JSON, use json.dumps.

import json
my_dict = {u'document_tone': {u'tone_categories': []}}
my_json = json.dumps(my_dict)

This will result in the expected output:

{"document_tone": {"tone_categories": []}}
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • i did this and was able to remove "u" from it. but when i entered a loop i keep getting error TypeError: string indices must be integers, not str my loop is : for i in json_final2['document_tone']['tone_categories']: print(i['category_name']) print("-" * len(i['category_name'])) for j in i['tones']: print(j['tone_name'].ljust(20), (str(round(j['score'] * 10, 1))).rjust(10)) print() print() – Yash Bhagwatkar Sep 07 '17 at 07:08
  • JSON is a string representation of a data structure. So json.dumps just returns a flat string, you can't iterate over it like that. – Daniel Hepper Sep 07 '17 at 07:28
  • @YashBhagwatkar please do NOT edit a post to replace it's whole content with a totally different question. – bruno desthuilliers Sep 07 '17 at 07:41
  • you can use `json.loads` like my_json = json.loads(json.dumps(my_dict))..Then you will get the result when it loop @Yash Bhagwatkar – lalithkumar Sep 07 '17 at 09:22
1

That's not json, it's python dict, you can use json.dumps to convert dict to json string

import json
d = {u'document_tone': {u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.573181, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.07296, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.129168, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.058345, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.395484, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.9762, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.733641, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.495667, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.194302, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.705757, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.189904, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.850804, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, u'sentences_tone': [{u'input_to': 129, u'text': u'The argument states that as the physical injury occurring on the job increases, the wages paid to employees should also increase.', u'sentence_id': 0, u'input_from': 0, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.261539, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.209928, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.114434, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.054893, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.360565, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.398794, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.0, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.341757, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.53574, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.617458, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.512805, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.944333, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 249, u'text': u'Hence it makes sense for employers to make the workplace safer, thereby reducing their payroll expenses and save money.', u'sentence_id': 1, u'input_from': 130, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.404165, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.060763, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.162228, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.216902, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.149858, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.952597, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.0, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.186754, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.304324, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.705324, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.41322, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.890612, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 312, u'text': u'Stated in this way the argument is incomplete and unjustified.', u'sentence_id': 2, u'input_from': 250, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.489648, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.060676, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.043002, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.066779, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.55297, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.896021, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.497569, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.844606, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.258585, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.608366, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.077368, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.209902, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 366, u'text': u'The argument is confusing causality with correlation.', u'sentence_id': 3, u'input_from': 313, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.46968, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.030388, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.161173, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.065839, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.449657, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.974578, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.91961, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.914923, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.099889, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.769632, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.312134, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.062347, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}]}
json.dumps(d)
BohanZhang
  • 163
  • 7
-3

This is my answer for your problem

my_str="your massive string here"

data=my_str.replace("u'","").replace("social_tone","").replace("Social Tone","")

print data
Anurag Misra
  • 1,516
  • 18
  • 24
Kiba
  • 1
  • 1