Solved

JSON - How to search and replace values

Posted on 2015-01-04
2
190 Views
Last Modified: 2015-01-04
How can I search and replace values in JSON?
I want to replace the values in the 'docs' with the values in the 'highlighting'.  So I can bind the highlighted with the <em> tag.

Any ideas?
thanks




            var data = [
                {
                    "responseHeader":{
                        "status":0,
                        "QTime":10,
                        "params":{
                            "wt":"json",
                            "facet":"true",
                            "hl.fl":"name,features",
                            "facet.mincount":"1",
                            "facet.field":"cat",
                            "indent":"on",
                            "hl":"true",
                            "q":"video"}},
                    "response":{"numFound":3,"start":0,"docs":[
                           {
                               "id":"MA147LL/A",
                               "sku":"MA147LL/A",
                               "name":"Apple 60 GB iPod with Video Playback Black",
                               "manu":"Apple Computer Inc.",
                               "includes":"earbud headphones, USB cable",
                               "weight":5.5,
                               "price":399.0,
                               "popularity":10,
                               "inStock":true,
                               "timestamp":"2007-01-31T05:12:44.562Z",
                               "cat":[
                                "electronics",
                                "music"],
                               "features":[
                                "iTunes, Podcasts, Audiobooks",
                                "Stores up to 15,000 songs, 25,000 photos, or 150 hours of video",
                                "2.5-inch, 320x240 color TFT LCD display with LED backlight",
                                "Up to 20 hours of battery life",
                                "Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video",
                                "Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication"]},
                           {
                               "id":"EN7800GTX/2DHTV/256M",
                               "sku":"EN7800GTX/2DHTV/256M",
                               "name":"ASUS Extreme N7800GTX/2DHTV (256 MB)",
                               "manu":"ASUS Computer Inc.",
                               "weight":16.0,
                               "price":479.95,
                               "popularity":7,
                               "inStock":false,
                               "timestamp":"2007-01-31T05:12:45.187Z",
                               "cat":[
                                "electronics",
                                "graphics card"],
                               "features":[
                                "NVIDIA GeForce 7800 GTX GPU/VPU clocked at 486MHz",
                                "256MB GDDR3 Memory clocked at 1.35GHz",
                                "PCI Express x16",
                                "Dual DVI connectors, HDTV out, video input",
                                "OpenGL 2.0, DirectX 9.0"]},
                           {
                               "id":"100-435805",
                               "sku":"100-435805",
                               "name":"ATI Radeon X1900 XTX 512 MB PCIE Video Card",
                               "manu":"ATI Technologies",
                               "weight":48.0,
                               "price":649.99,
                               "popularity":7,
                               "inStock":false,
                               "timestamp":"2007-01-31T05:12:45.203Z",
                               "cat":[
                                "electronics",
                                "graphics card"],
                               "features":[
                                "ATI RADEON X1900 GPU/VPU clocked at 650MHz",
                                "512MB GDDR3 SDRAM clocked at 1.55GHz",
                                "PCI Express x16",
                                "dual DVI, HDTV, svideo, composite out",
                                "OpenGL 2.0, DirectX 9.0"]}]
                    },
                    "facet_counts":{
                        "facet_queries":{},
                        "facet_fields":{
                            "cat":[
                             "electronics",3,
                             "card",2,
                             "graphics",2,
                             "music",1]}},
                    "highlighting":{
                        "MA147LL/A":{
                            "name":["Apple 60 GB iPod with <em>Video</em> Playback Black"],
                            "features":["Stores up to 15,000 songs, 25,000 photos, or 150 hours of <em>video</em>"]},
                        "EN7800GTX/2DHTV/256M":{
                            "features":["Dual DVI connectors, HDTV out, <em>video</em> input"]},
                        "100-435805":{
                            "name":["ATI Radeon X1900 XTX 512 MB PCIE <em>Video</em> Card"]}}          
                }];
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40530475
Maybe something like this:
  $.each(data, function(idx1, obj1) {
    $.each(obj1.highlighting, function(idx2, obj2) {
      $.each(obj1.response.docs, function(idx3, obj3) {
        if (obj3.id == idx2) {
          $.each(obj2, function(idx4, obj4) {
            if (idx4 in obj3) {
              obj3[idx4] = obj2[idx4];
            }
          });
        }
      });
    });
  });

Open in new window

I also made a jsfiddle to show the results.
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40530595
Awesome!  again!
thx
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question